From 07d057cb63ff29bee7aa1e4a4c42bf058a6c2206 Mon Sep 17 00:00:00 2001 From: Adam Buechler Date: Fri, 21 Jun 2024 15:05:50 +0900 Subject: [PATCH 1/2] Add failing type tests for first() and last() --- type-definitions/flow-tests/immutable-flow.js | 10 ++++++++++ type-definitions/ts-tests/list.ts | 20 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/type-definitions/flow-tests/immutable-flow.js b/type-definitions/flow-tests/immutable-flow.js index 4b32d64341..c203962052 100644 --- a/type-definitions/flow-tests/immutable-flow.js +++ b/type-definitions/flow-tests/immutable-flow.js @@ -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] diff --git a/type-definitions/ts-tests/list.ts b/type-definitions/ts-tests/list.ts index 1697d62042..08ea834568 100644 --- a/type-definitions/ts-tests/list.ts +++ b/type-definitions/ts-tests/list.ts @@ -76,6 +76,26 @@ import { get(List(), 4, 'a'); } +{ + // #first + + // $ExpectType number | undefined + List().first(); + + // $ExpectError + (): number => List().first(); +} + +{ + // #last + + // $ExpectType number | undefined + List().last(); + + // $ExpectError + (): number => List().last(); +} + { // #set From 8b3cd075d55f6488c63c7e51fda06d02c376302a Mon Sep 17 00:00:00 2001 From: Adam Buechler Date: Fri, 21 Jun 2024 15:06:10 +0900 Subject: [PATCH 2/2] Fix types of first() and last() --- type-definitions/immutable.d.ts | 6 ++++-- type-definitions/immutable.js.flow | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/type-definitions/immutable.d.ts b/type-definitions/immutable.d.ts index dc2b24547c..3fd60eecb9 100644 --- a/type-definitions/immutable.d.ts +++ b/type-definitions/immutable.d.ts @@ -4208,7 +4208,8 @@ 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(notSetValue?: NSV): V | NSV; + first(notSetValue: NSV): V | NSV; + first(): V | undefined; /** * In case the `Collection` is not empty returns the last element of the @@ -4216,7 +4217,8 @@ declare namespace Immutable { * In case the `Collection` is empty returns the optional default * value if provided, if no default value is provided returns undefined. */ - last(notSetValue?: NSV): V | NSV; + last(notSetValue: NSV): V | NSV; + last(): V | undefined; // Reading deep values diff --git a/type-definitions/immutable.js.flow b/type-definitions/immutable.js.flow index 67a496f29c..901960b873 100644 --- a/type-definitions/immutable.js.flow +++ b/type-definitions/immutable.js.flow @@ -76,8 +76,10 @@ declare class _Collection implements ValueObject { has(key: K): boolean; includes(value: V): boolean; contains(value: V): boolean; - first(notSetValue?: NSV): V | NSV; - last(notSetValue?: NSV): V | NSV; + first(): V | void; + first(notSetValue: NSV): V | NSV; + last(): V | void; + last(notSetValue: NSV): V | NSV; hasIn(keyPath: Iterable): boolean;