为什么typescript只在将字符串赋给数字时显示错误,而在将null赋给数字时不显示错误?
export type ArrayWithNumberOrString = Array<number | string>;
export type ArrayWithNumberOrNull = Array<number | null>;
function doSomethingWithNumbersOnly(array: number[]): number[] {
return array;
};
const numbersAndString: ArrayWithNumberOrString = [1, 2, 3, 'string'];
const numbersAndNull: ArrayWithNumberOrNull = [1, 2, 3, null];
doSomethingWithNumbersOnly(numbersAndString);
doSomethingWithNumbersOnly(numbersAndNull);
Typescript sandbox
最佳答案
立刻找到了答案:
默认情况下,tsconfig.json中的选项strictNullChecks
被禁用。