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
A type guard in a loop does not narrow an array element to its type inside the conditional.
On the opposite, the same works by accessing the array element using a variable in between.
This could be by design, but it's quite strange to me...
Code
declarefunctionisNum(a: any): a is numberdeclarefunctionacceptNum(c: number): voidletchildren: Array<string|number>=[3,4,5]for(leti=0;i<children.length;++i){if(isNum(children[i])){acceptNum(children[i]);// error: Type string is not assignatable to type number }}for(leti=0;i<children.length;++i){constchild=children[i];if(isNum(child)){acceptNum(child);// no error}}
TypeScript Version: 2.2.1
A type guard in a loop does not narrow an array element to its type inside the conditional.
On the opposite, the same works by accessing the array element using a variable in between.
This could be by design, but it's quite strange to me...
Code
TS play demo here
Expected behavior:
children[i]
type narrowed tonumber
inside theif
body.Actual behavior:
children[i]
type is stillstring | number
inside theif
body.The text was updated successfully, but these errors were encountered: