-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: incorrect tuple (array) type after changing in place #52375
Comments
The in-place mutation methods for arrays aren't supported from a control flow perspective. I recommend using |
types can change because a different type is assigned (example), would it be possible to add some type annotation to class methods to tell what happens to the instance type? |
I don't think widening the declared type via control-flow-analysis is reasonable in the same way as narrowing. But it is a problem that TypeScript allows operations that invalidate the underlying type annotations. e.g. this is a type error: let x: [number, string] = [1,'a']
x = [x[1], x[0]] This is (almost) the same thing, but no type error: let x: [number, string] = [1,'a']
x.reverse() Non-readonly tuples are dangerously unsound. Given that this won't be fixed (per #6325), I'd avoid them completely. |
Bug Report
after you change an array in place (reverse, splice, push, pop, etc...) tuple types are unchanged and still reflect the type before the change
🔎 Search Terms
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
type is unchanged
🙂 Expected behavior
after Array.reverse() the type should update if the type specifies the order of the elements' types (like
[string, number]
)I also was playing around with a type that reverses a tuple type with some usage examples. this worked out pretty well, this could be useful for the Array.reverse type declaration, see the linked ts playground code
The text was updated successfully, but these errors were encountered: