TS2367:此条件将始终与任何条件一起返回'false'>>

问题描述 投票:0回答:1

我很难理解为什么编译器从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(...

typescript typescript2.0
1个回答
1
投票

[JavaScript / TypeScript中没有null类型。

console.log(typeof null); // "object"
© www.soinside.com 2019 - 2024. All rights reserved.