我通常有以下代码:
class Foo {
foo: SomeType[];
doSomething() {
const a = this.foo = [];
}
}
在这种情况下,a将是any[]or never[](取决于环境)而不是SomeType[]. 如果我指定noImplicitAny那些暗示any[],编译器会抛出一个错误。
我知道下面的演员表解决了这个问题,但为什么 TypeScript 不能从中推断出类型this.foo?
const a: SomeType[] = this.foo = []; // Have to repeat the type again
可重现的代码:
tsconfig.json:
{
"compilerOptions": {
"noImplicitAny": true
}
}
test.ts:
class Foo {
foo: number[];
doSomething() {
const a = this.foo = [];
}
}
