You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// test.tsinterfaceType{type: number;}interfaceTypeExtextendsType{arr: Type[];}constguard=(arg: Type): arg is TypeExt=>arg.type===1;constotherFunc=(arg1: Type,arg2: TypeExt): void=>{};exportfunctiony(arg: Type): void{if(guard(arg)){for(constITEM/* error is here */ofarg.arr){if(otherFunc(ITEM,arg)){}}}}
Compiled with cmd: tsc --noImplicitAny test.ts
Expected behavior:
No errors
Actual behavior:
test.ts(14,16): error TS7022: 'ITEM' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
The text was updated successfully, but these errors were encountered:
This is a control flow analysis issue. In order to infer a type for ITEM we need to figure out the control flow type of arg.arr. That in turn means we need to look at constructs that affect the type of arg. This includes the call to otherFunc which looks like it could be a user defined type predicate. In order to determine if it is a type predicate, we resolve the call expression, which entails resolving the argument expressions, which requires us to know the type of ITEM. This creates a circularity we can't resolve, so we fall back to an implicit any type.
We can fix this by having the type predicate recognition logic first resolve the type of the function object and examine all of the call signatures. If none of them are user defined type predicates, we can bail out early without resolving the call argument expressions, which avoids the circularity.
Meanwhile, you can break the circularity simply by adding parentheses to arg in the call to otherFunc. In other words, change the call to otherFunc(ITEM, (arg)). This causes the control flow analyzer to no longer consider the call a possible user defined type predicate invocation.
TypeScript Version: 2.0.8
Code
Compiled with cmd:
tsc --noImplicitAny test.ts
Expected behavior:
No errors
Actual behavior:
The text was updated successfully, but these errors were encountered: