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
tuple, flow analysis, constant index, type narrowing
Suggestion
Currently flow analysis works for object properties, but not for tuple/array items (or even objects with explicit numeric-string-keys (obj['0']) according to my quick experiment in the playground). It seems to me that this should at the very least work for accessing a constant index.
My suggestion meets these guidelines:
[x] This wouldn't be a breaking change in existing TypeScript / JavaScript code
[x] This wouldn't change the runtime behavior of existing JavaScript code
[x] This could be implemented without emitting different JS based on the types of the expressions
[x] This isn't a runtime feature (e.g. new expression-level syntax)
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
Uh oh!
There was an error while loading. Please reload this page.
Search Terms
tuple, flow analysis, constant index, type narrowing
Suggestion
Currently flow analysis works for object properties, but not for tuple/array items (or even objects with explicit numeric-string-keys (
obj['0']
) according to my quick experiment in the playground). It seems to me that this should at the very least work for accessing a constant index.Putting this in code terms
The
numObject
case is particularly surprising to me.Playground link (requires strict mode):
https://www.typescriptlang.org/play/index.html#src=function%20takesNumber(n%3A%20number)%20%7B%20%7D%0D%0A%0D%0Aconst%20object%3A%20%7B%20x%3A%20number%20%7C%20null%20%7D%20%3D%20%7B%20x%3A%2010%20%7D%3B%0D%0Aconst%20numObject%3A%20%7B%20'0'%3A%20number%20%7C%20null%20%7D%20%3D%20%7B%20'0'%3A%2010%20%7D%3B%0D%0Aconst%20tuple%3A%20%5Bnumber%20%7C%20null%5D%20%3D%20%5B10%5D%3B%0D%0A%0D%0Aif%20(object.x)%20takesNumber(object.x)%3B%20%20%2F%2F%20OK%0D%0Aif%20(numObject%5B'0'%5D)%20takesNumber(numObject%5B'0'%5D)%3B%20%20%2F%2F%20Error%0D%0Aif%20(tuple%5B0%5D)%20takesNumber(tuple%5B0%5D)%3B%20%20%2F%2F%20Error%0D%0A
Use Cases
I hit this issue when trying to refactor:
to
Checklist
My suggestion meets these guidelines:
[x] This wouldn't be a breaking change in existing TypeScript / JavaScript code
[x] This wouldn't change the runtime behavior of existing JavaScript code
[x] This could be implemented without emitting different JS based on the types of the expressions
[x] This isn't a runtime feature (e.g. new expression-level syntax)
Potentially related issues
#23512
#12849
#14957
I don't know enough about the compiler to know whether these address the same issue.
The text was updated successfully, but these errors were encountered: