Skip to content
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

Could handling of Array pop() and shift() be made better? #9249

Closed
Dagur opened this issue Jan 25, 2025 · 1 comment
Closed

Could handling of Array pop() and shift() be made better? #9249

Dagur opened this issue Jan 25, 2025 · 1 comment

Comments

@Dagur
Copy link
Contributor

Dagur commented Jan 25, 2025

Proposal

In 0.239.x a change was made

Calling shift() and pop() on an Array will now return T | undefined. #8341

Which is great but I was wondering if it could be made a little bit smarter because it leads to checks and $FlowFixMe's that shouldn't be needed.

Use case

Example:

const a = someString.split(",").pop()
Here we know for certain that a is a string because split() will return a non-empty array of strings.

Example 2:

while (stringArray.length > 0) {
  const a = stringArray.shift();
 ...
}

We have made sure that the array is not empty but Flow still insists that a could be undefined.

@Dagur Dagur changed the title Can Could handling of Array pop() and shift() be made better? Jan 25, 2025
@SamChou19815
Copy link
Contributor

I don't think it can be improved, without hardcoding a lot of things just for this case. Then there is also the similar problem with the Map.has Map.get pair, and so on. We decided that it's better to accept the limitation and make the typing more sound, rather than keeping it unsound and hoping that one day we can come up with something that make most of the existing code still type check

@SamChou19815 SamChou19815 closed this as not planned Won't fix, can't repro, duplicate, stale Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants