Skip to content

Fix type inference for first() and last() #2001

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

Merged
merged 2 commits into from
Jul 22, 2024

Conversation

butchler
Copy link
Contributor

Problem

Because of the way types are inferred in both TypeScript and Flow, it's possible for the NSV (Not-set value) of the first() and last() methods to be inferred incorrectly. For example:

const good = List<number>().first(); // Type is properly inferred as number | undefined, because the list may be empty
const bad: number = List<number>().first(); // This is not a type error, even though the types should not be compatible

This is quite subtle and it's easy for it to go unnoticed (for example, this went unnoticed for several years in our company's moderately large TypeScript codebase with ~2000 TypeScript files and ~200,000 lines of code). But it can cause bugs because it makes it easy to accidentally ignore the fact that the list may be empty.

Fix

This PR adds type tests for the above case, and fixes the types by updating first() and last() to use method overloads for the no-argument case. Note that this makes the types the same as the get() method, which already uses a method overload and doesn't suffer from this problem.

Technically this is a breaking change to the types: if this change is released, people will likely get some type errors when they upgrade to the version with the fix. However, those should all be places where there is a potential bug due to ignoring the possibility of the list being empty. If people want to ignore the error, they can do so using a ! assertion (e.g. .first()!).

Prior art

first(): V | void;
first<NSV>(notSetValue: NSV): V | NSV;
last(): V | void;
last<NSV>(notSetValue: NSV): V | NSV;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've never used Flow before. Is this the correct way to define these types?

Copy link
Member

@jdeniau jdeniau Jul 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never used flow either.
Honestly, I'd like to remove flow types from the main immutable repository as there is no maintenance on it :/ (and I think that flow did loose the battle against typescript)

@jdeniau
Copy link
Member

jdeniau commented Jul 22, 2024

I will rebase this on 5.x as :

  • the type testing system did change, and 5.x major breaking change is on TS,
  • the 5.x will be released "soon"

@jdeniau jdeniau changed the base branch from main to 5.x July 22, 2024 12:54
@jdeniau jdeniau merged commit f7849ac into immutable-js:5.x Jul 22, 2024
5 checks passed
@jdeniau
Copy link
Member

jdeniau commented Jul 22, 2024

This has been released in 5.0.0-rc.1.

Thank you

@butchler
Copy link
Contributor Author

Thank you!

@butchler butchler deleted the first-last-types branch July 23, 2024 03:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants