即使我明确检查未定义,我也有一个简单的情况,strictNullChecks
即不缩小类型。
interface WideType {
one: { two: number } | undefined;
}
interface NarrowType {
one: { two: number };
}
const example = (value: WideType): NarrowType | null => {
if (value.one) {
return value; // I have an error here
}
return null;
};
错误信息是:
Type 'WideType' is not assignable to type 'NarrowType'.
Types of property 'one' are incompatible.
Type '{ two: number; } | undefined' is not assignable to type '{ two: number; }'.
Type 'undefined' is not assignable to type '{ two: number; }'.ts(2322)
我如何帮助 TS 编译器解决这个问题?TS 版本 3.7.2