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
You didn't post a full example, but I'm assuming columnIndex is a mutable variable (i.e. declared with let or var). If so, the compiler doesn't consider the guard against null to be in effect within the arrow function (because there could have been a subsequent assignment to columnIndex before the arrow function is called).
It should work if you declare columnIndex as const. See #8849 and #10357.
Good point about columnIndex not being const. However the type checker could ideally still infer that columnIndex can not be accessed by anything than the lambda closure (and therefor can not possible be null), given that columnIndex is a primitive type (i.e. not a reference to an object), and the lambda is in a return statement (after which columnIndex goes out of scope). So maybe this is more of a "possible improvement to the type checker" issue then.
TypeScript Version: 2.5.3
Code
Expected behavior:
No type errors.
Actual behavior:
error TS2538: Type 'null' cannot be used as an index type.
(referring to the columnIndex variable captured by the lambda expression)
Work around:
The type error goes away if I instead do:
return (rowIndex:number) => table.rows[rowIndex].rowValues[columnIndex as number];
However that shouldn't be necessary.
The text was updated successfully, but these errors were encountered: