I usually have the following code:
class Foo {
foo: SomeType[];
doSomething() {
const a = this.foo = [];
}
}
In this case, a would be any[] or never[] (depends on environment) instead of SomeType[]. If I specify noImplicitAny on those that imply any[], the compiler would throw an error.
I know the below cast fixes the problem, but why can't TypeScript deduce the type from this.foo?
const a: SomeType[] = this.foo = []; // Have to repeat the type again
Reproducible code:
tsconfig.json:
{
"compilerOptions": {
"noImplicitAny": true
}
}
test.ts:
class Foo {
foo: number[];
doSomething() {
const a = this.foo = [];
}
}

The type being inferred as
any[]makes sense because Javascript is right-associative wrt theassignmentoperator.See this question: Multiple left-hand assignment with JavaScript
This means that the expression:
Is interpreted as:
As you can see, the type information isn't associated with empty array, so
any[]is the most correct type.You can prove this is in fact what is happening with a simple example:
The inferred type for
awould be the literal number5, notnumber(which is the type fort).It seems that the typescript playground may be wrong in this case, which is why many (including myself) are reporting
never[]as the inferred type.