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
TypeScript does not correctly infer the return type of Array.pop when called on a nonempty array.
I have seen issue #30406, and the comment stating that this issue "cannot be tracked", but I'm unsure what that means and whether it applies where an array is guaranteed to be nonempty such as in the cases below.
If anything there is an in consistency between using pop and indexing with arr.length - 1. Ideally I would like to see pop be inferred correctly, but consistent behaviour between pop and indexing would be the next best thing.
The type system has no information that the arrays are really not empty, and the type checker works based on the type system. Only tuples are guaranteed to not be empty, but they're not mutable, so no pop method.
As for consistent behavior between pop and indexing: that’s why noUncheckedIndexedAccess was introduced.
And yeah, “not tracked” means what it sounds like, TS just sees “called .pop() on something of type number[]” which isn’t enough information to guarantee a non-nullish result; that you called it on an array literal is irrelevant because TS doesn’t have a “non-empty array” type.
Bug Report
TypeScript does not correctly infer the return type of
Array.pop
when called on a nonempty array.I have seen issue #30406, and the comment stating that this issue "cannot be tracked", but I'm unsure what that means and whether it applies where an array is guaranteed to be nonempty such as in the cases below.
If anything there is an in consistency between using
pop
and indexing witharr.length - 1
. Ideally I would like to seepop
be inferred correctly, but consistent behaviour betweenpop
and indexing would be the next best thing.🔎 Search Terms
🕗 Version & Regression Information
I am using
"target": "ESNext"
and"module": "ESNext"
⏯ Playground Link
Playground Link
💻 Code
🙁 Actual behavior
🙂 Expected behavior
Or
The text was updated successfully, but these errors were encountered: