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
{{ message }}
Repository navigation
Incorrect type inference of tuple when underlying array is extended #13941
changed the title [-]Incorrect type inference of tuple when it is extended[/-][+]Incorrect type inference of tuple when underlying array is extended[/+]on Feb 7, 2017
Using --strictNullChecks and typescript@2.1.5, the type of y is string | number, outside of the if statement, and never inside of it.
The inference of never is correct and expected because y is not not nullable, and therefore the block will never execute.
With respect to the inferred type of the array element, I believe it is covered by the proposal in #6229
Andrey Fedorov (@anfedorov) Ah, I see your point now. I misread it as y === null which would not be taken. It seems there may be a bug here, but I am not sure.
unfortunately there is no way to do this correctly in the general case, e.g.: for (var z = 0, z<10, z++) x[z];. you can also find more details in #13778 on why the index signature intentionally, does not include undefined.
copying tuple behavior from Python seems like one way to handle it. i.e. x[3] = 'hi' could raise an error. another option is for x[3] = 'hi' to mutate the type of x to [string, number, undefined, string], fixing the error inside the if block.
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
Activity
[-]Incorrect type inference of tuple when it is extended[/-][+]Incorrect type inference of tuple when underlying array is extended[/+]aluanhaddad commentedon Feb 8, 2017
Using
--strictNullChecksand typescript@2.1.5, the type ofyisstring | number, outside of theifstatement, andneverinside of it.The inference of
neveris correct and expected becauseyis not not nullable, and therefore the block will never execute.With respect to the inferred type of the array element, I believe it is covered by the proposal in #6229
anfedorov commentedon Feb 8, 2017
But the block does execute and the code compiles without warnings or errors, even with
--strictNullChecks:Thanks for the reference to the proposal — that is indeed a little closer to the behavior I would have expected.
aluanhaddad commentedon Feb 8, 2017
Andrey Fedorov (@anfedorov) Ah, I see your point now. I misread it as
y === nullwhich would not be taken. It seems there may be a bug here, but I am not sure.anfedorov commentedon Feb 8, 2017
Yup,
yisundefined, and the proposal you linked might be the most elegant fix.mhegazy commentedon Apr 27, 2017
unfortunately there is no way to do this correctly in the general case, e.g.:
for (var z = 0, z<10, z++) x[z];. you can also find more details in #13778 on why the index signature intentionally, does not includeundefined.anfedorov commentedon Apr 28, 2017
copying tuple behavior from Python seems like one way to handle it. i.e.
x[3] = 'hi'could raise an error. another option is forx[3] = 'hi'to mutate the type ofxto[string, number, undefined, string], fixing the error inside the if block.this seems to work fine in the playground: https://www.typescriptlang.org/play/index.html#src=let%20x%3A%20%5Bnumber%2C%20string%2C%20undefined%2C%20number%5D%3B%0D%0Ax%20%3D%20%5B1%2C%20'1'%2C%20undefined%2C%202%5D%3B%0D%0A%0D%0Ax%5B3%5D%20%3D%202%0D%0Ax%5B2%5D%20%3D%203%0D%0A
regardless,
x[z]just likex[prompt()]could bestring | number | undefined?mhegazy commentedon May 22, 2017
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.