根据我的理解,这不应该是可能的:
class UndefinedStringFactory {
universalValue: never;
constructor() {}
build(): string {
return this.universalValue;
}
}
let x: string = new UndefinedStringFactory().build();
console.log(x); // Didn't know that undefined is of type string...
即使使用strict
编译器选项(Typescript 2.7.1和2.8.0-dev.20180206)也可以。当我删除空构造函数时,它不会按预期编译。