TypeScript thinks array values are fixed to the values from initialization.
It doesn't actually think this - it optimistically assumes the .shift() call has no side effects and therefore that the previous narrowing check (x[0] === 1) hasn't been invalidated.
workinggrafton commentedMar 10, 2022
Bug Report
TypeScript doesn't understand that array shift mutates the values of the array.
https://www.typescriptlang.org/play?#code/MYewdgzgLgBAHjAvDA2gRgDQwExYMwC6A3AFAkCWAZjABRwoAMBSiyaAlDAN4kzwB0EABZUoNdqT5Va9Ji2TZOPPn1CQQAGwCm-DSADmNAOQgA1kYm8YAXxLWgA
Array
shift
unshift
Happens on nightly, currently failing on 4.7.0 also.
https://www.typescriptlang.org/play?#code/MYewdgzgLgBAHjAvDA2gRgDQwExYMwC6A3AFAkCWAZjABRwoAMBSiyaAlDAN4kzwB0EABZUoNdqT5Va9Ji2TZOPPn1CQQAGwCm-DSADmNAOQgA1kYm8YAXxLWgA
Error evaluating x[0] after a shift. TypeScript thinks array values are fixed to the values from initialization.
Error evaluating x[0] is typed as 'number', not a numeric literal (i.e. no error in example, console statement is reachable and prints).
The text was updated successfully, but these errors were encountered: