-
Notifications
You must be signed in to change notification settings - Fork 13k
Closed
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed
Description
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
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
⏯ Playground Link
Playground link with relevant code
💻 Code
function getNumber(): [ string, number ] {
if (Math.random() < 0.5) {
return [ 'pi constant', Math.PI ];
} else {
return [ 'random number', Math.random() ];
}
}
let a: number;
const s = getNumber();
a = s[1];
// @ts-expect-error
// [ string, number ] -> (string | number)[]
// Type 'string | number' is not assignable to type 'number'.
a = s.reverse()[0];
// @ts-expect-error
// After s got reversed in place, it is a [ number, string ], but typeof s = [ string, number ]
// Type 'string' is not assignable to type 'number'.
a = s[0];
🙁 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
Metadata
Metadata
Assignees
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed