我很难理解为什么编译器从null
可能的类型集中排除arg
(或者我不理解诊断消息)。>>
static vetStringNullable(arg:any): string|null { if (typeof arg === 'string') { return arg; } if (typeof arg === 'null') { return arg; } throw TypeError('Vetted value is not of type string or null.'); }
上面的代码产生以下编译器诊断:
TS2367: This condition will always return 'false' since the types '"string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"' and '"null"' have no overlap.
我很难理解为什么编译器会从arg可能的类型集中排除null(或者,我不理解诊断消息)。静态vetStringNullable(...
[JavaScript / TypeScript中没有null
类型。
console.log(typeof null); // "object"