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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions type-definitions/flow-tests/immutable-flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ var item: number = numberList.get(4);
var nullableItem: ?number = numberList.get(4);
var itemOrDefault: number = numberList.get(4, 10);

// $FlowExpectedError[incompatible-type]
var item: number = numberList.first();
// $FlowExpectedError[incompatible-type]
var func: () => number = () => numberList.first();

// $FlowExpectedError[incompatible-type]
var item: number = numberList.last();
// $FlowExpectedError[incompatible-type]
var func: () => number = () => numberList.last();

numberList = List().insert(0, 0);
numberOrStringList = List.of(0).insert(1, 'a');
// $FlowExpectedError[incompatible-call]
Expand Down
6 changes: 4 additions & 2 deletions type-definitions/immutable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4208,15 +4208,17 @@ declare namespace Immutable {
* In case the `Collection` is empty returns the optional default
* value if provided, if no default value is provided returns undefined.
*/
first<NSV = undefined>(notSetValue?: NSV): V | NSV;
first<NSV>(notSetValue: NSV): V | NSV;
first(): V | undefined;

/**
* In case the `Collection` is not empty returns the last element of the
* `Collection`.
* In case the `Collection` is empty returns the optional default
* value if provided, if no default value is provided returns undefined.
*/
last<NSV = undefined>(notSetValue?: NSV): V | NSV;
last<NSV>(notSetValue: NSV): V | NSV;
last(): V | undefined;

// Reading deep values

Expand Down
6 changes: 4 additions & 2 deletions type-definitions/immutable.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ declare class _Collection<K, +V> implements ValueObject {
has(key: K): boolean;
includes(value: V): boolean;
contains(value: V): boolean;
first<NSV>(notSetValue?: NSV): V | NSV;
last<NSV>(notSetValue?: NSV): V | NSV;
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)


hasIn(keyPath: Iterable<mixed>): boolean;

Expand Down
20 changes: 20 additions & 0 deletions type-definitions/ts-tests/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,26 @@ import {
get(List<number>(), 4, 'a');
}

{
// #first

// $ExpectType number | undefined
List<number>().first();

// $ExpectError
(): number => List<number>().first();
}

{
// #last

// $ExpectType number | undefined
List<number>().last();

// $ExpectError
(): number => List<number>().last();
}

{
// #set

Expand Down
Loading