Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ca565f1

Browse files
committed
Add '| undefined' to TypeScript definitions, which can return undefined
TypeScript supports non nullable types (optionally), and it's very helpful to know when some function or method could return `undefined` as well as a valid result. This PR adds `| undefined` to the return types of the methods, which can return `undefined` (like, `get`, `find`, etc).
1 parent 3dfef98 commit ca565f1

File tree

4 files changed

+77
-65
lines changed

4 files changed

+77
-65
lines changed

dist/immutable-nonambient.d.ts

+40-28
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@
141141
* ```js
142142
* List.of(1, 2, 3, 4).toJS();
143143
* // [ 1, 2, 3, 4 ]
144+
*
145+
* List.of({x:1}, 2, [3], 4).toJS();
146+
* // [ { x: 1 }, 2, [ 3 ], 4 ]
144147
* ```
145148
*/
146149
function of<T>(...values: T[]): List<T>;
@@ -155,12 +158,20 @@
155158
*
156159
* const plainArray = [1, 2, 3, 4];
157160
* const listFromPlainArray = List(plainArray);
161+
*
162+
* const plainSet = new Set([1, 2, 3, 4]);
158163
* const listFromPlainSet = List(plainSet);
159-
* const listFromIterableArray = List(plainArray[Symbol.iterator]());
164+
*
165+
* const iterableArray = plainArray[Symbol.iterator]();
166+
* const listFromIterableArray = List(iterableArray);
160167
*
161168
* listFromPlainArray.toJS(); // [ 1, 2, 3, 4 ]
162169
* listFromPlainSet.toJS(); // [ 1, 2, 3, 4 ]
163170
* listFromIterableArray.toJS(); // [ 1, 2, 3, 4 ]
171+
*
172+
* Immutable.is(listFromPlainArray, listFromIterableSet); // true
173+
* Immutable.is(listFromPlainSet, listFromIterableSet) // true
174+
* Immutable.is(listFromPlainSet, listFromPlainArray) // true
164175
* ```
165176
*/
166177
export function List<T>(): List<T>;
@@ -191,7 +202,8 @@
191202
* originalList.set(1, 1).toJS(); // [ 0, 1 ]
192203
* originalList.set(0, 'overwritten').toJS(); // [ 'overwritten' ]
193204
*
194-
* List().set(50000, 'value').size; // 50001
205+
* List().set(50000, 'value').size;
206+
* //50001
195207
* ```
196208
*/
197209
set(index: number, value: T): List<T>;
@@ -210,8 +222,8 @@
210222
* @alias remove
211223
*
212224
* ```js
213-
* List([10, 11, 12, 13, 14]).delete(0).toJS();
214-
* // [ 11, 12, 13, 14 ]
225+
* List([0, 1, 2, 3, 4]).delete(0).toJS();
226+
* // [ 1, 2, 3, 4 ]
215227
* ```
216228
*/
217229
delete(index: number): List<T>;
@@ -224,8 +236,8 @@
224236
* This is synonymous with `list.splice(index, 0, value)
225237
*
226238
* ```js
227-
* List([10, 11, 12, 13, 15]).insert(5, 14).toJS();
228-
* // [ 10, 11, 12, 13, 14, 15 ]
239+
* List([0, 1, 2, 3, 4]).insert(6, 5).toJS();
240+
* // [ 0, 1, 2, 3, 4, 5 ]
229241
* ```
230242
*/
231243
insert(index: number, value: T): List<T>;
@@ -270,8 +282,8 @@
270282
* values ahead to higher indices.
271283
*
272284
* ```js
273-
* List([11, 12, 13]).unshift(10).toJS();
274-
* // [ 10, 11, 13, 14 ]
285+
* List([ 2, 3, 4]).unshift(1).toJS();
286+
* // [ 1, 2, 3, 4 ]
275287
* ```
276288
*/
277289
unshift(...values: T[]): List<T>;
@@ -285,8 +297,8 @@
285297
* value in this List.
286298
*
287299
* ```js
288-
* List([10, 11, 12, 13, 14]).shift(0).toJS();
289-
* // [ 11, 12, 13, 14 ]
300+
* List([ 0, 1, 2, 3, 4]).shift(0).toJS();
301+
* // [ 1, 2, 3, 4 ]
290302
* ```
291303
*/
292304
shift(): List<T>;
@@ -1150,7 +1162,7 @@
11501162
/**
11511163
* Alias for `Stack.first()`.
11521164
*/
1153-
peek(): T;
1165+
peek(): T | undefined;
11541166

11551167

11561168
// Persistent changes
@@ -1691,7 +1703,7 @@
16911703
* `index` may be a negative number, which indexes back from the end of the
16921704
* Iterable. `s.get(-1)` gets the last item in the Iterable.
16931705
*/
1694-
get(index: number, notSetValue?: T): T;
1706+
get(index: number, notSetValue?: T): T | undefined;
16951707

16961708

16971709
// Conversion to Seq
@@ -1928,7 +1940,7 @@
19281940
* so if `notSetValue` is not provided and this method returns `undefined`,
19291941
* that does not guarantee the key was not found.
19301942
*/
1931-
get(key: K, notSetValue?: V): V;
1943+
get(key: K, notSetValue?: V): V | undefined;
19321944

19331945
/**
19341946
* True if a key exists within this `Iterable`, using `Immutable.is` to determine equality
@@ -1945,12 +1957,12 @@
19451957
/**
19461958
* The first value in the Iterable.
19471959
*/
1948-
first(): V;
1960+
first(): V | undefined;
19491961

19501962
/**
19511963
* The last value in the Iterable.
19521964
*/
1953-
last(): V;
1965+
last(): V | undefined;
19541966

19551967

19561968
// Reading deep values
@@ -2497,7 +2509,7 @@
24972509
predicate: (value: V, key: K, iter: /*this*/Iterable<K, V>) => boolean,
24982510
context?: any,
24992511
notSetValue?: V
2500-
): V;
2512+
): V | undefined;
25012513

25022514
/**
25032515
* Returns the last value for which the `predicate` returns true.
@@ -2508,7 +2520,7 @@
25082520
predicate: (value: V, key: K, iter: /*this*/Iterable<K, V>) => boolean,
25092521
context?: any,
25102522
notSetValue?: V
2511-
): V;
2523+
): V | undefined;
25122524

25132525
/**
25142526
* Returns the first [key, value] entry for which the `predicate` returns true.
@@ -2517,7 +2529,7 @@
25172529
predicate: (value: V, key: K, iter: /*this*/Iterable<K, V>) => boolean,
25182530
context?: any,
25192531
notSetValue?: V
2520-
): /*[K, V]*/Array<any>;
2532+
): /*[K, V]*/Array<any> | undefined;
25212533

25222534
/**
25232535
* Returns the last [key, value] entry for which the `predicate`
@@ -2529,15 +2541,15 @@
25292541
predicate: (value: V, key: K, iter: /*this*/Iterable<K, V>) => boolean,
25302542
context?: any,
25312543
notSetValue?: V
2532-
): /*[K, V]*/Array<any>;
2544+
): /*[K, V]*/Array<any> | undefined;
25332545

25342546
/**
25352547
* Returns the key for which the `predicate` returns true.
25362548
*/
25372549
findKey(
25382550
predicate: (value: V, key: K, iter: /*this*/Iterable.Keyed<K, V>) => boolean,
25392551
context?: any
2540-
): K;
2552+
): K | undefined;
25412553

25422554
/**
25432555
* Returns the last key for which the `predicate` returns true.
@@ -2547,17 +2559,17 @@
25472559
findLastKey(
25482560
predicate: (value: V, key: K, iter: /*this*/Iterable.Keyed<K, V>) => boolean,
25492561
context?: any
2550-
): K;
2562+
): K | undefined;
25512563

25522564
/**
25532565
* Returns the key associated with the search value, or undefined.
25542566
*/
2555-
keyOf(searchValue: V): K;
2567+
keyOf(searchValue: V): K | undefined;
25562568

25572569
/**
25582570
* Returns the last key associated with the search value, or undefined.
25592571
*/
2560-
lastKeyOf(searchValue: V): K;
2572+
lastKeyOf(searchValue: V): K | undefined;
25612573

25622574
/**
25632575
* Returns the maximum value in this collection. If any values are
@@ -2574,7 +2586,7 @@
25742586
* If `comparator` returns 0 and either value is NaN, undefined, or null,
25752587
* that value will be returned.
25762588
*/
2577-
max(comparator?: (valueA: V, valueB: V) => number): V;
2589+
max(comparator?: (valueA: V, valueB: V) => number): V | undefined;
25782590

25792591
/**
25802592
* Like `max`, but also accepts a `comparatorValueMapper` which allows for
@@ -2586,7 +2598,7 @@
25862598
maxBy<C>(
25872599
comparatorValueMapper: (value: V, key: K, iter: /*this*/Iterable<K, V>) => C,
25882600
comparator?: (valueA: C, valueB: C) => number
2589-
): V;
2601+
): V | undefined;
25902602

25912603
/**
25922604
* Returns the minimum value in this collection. If any values are
@@ -2603,7 +2615,7 @@
26032615
* If `comparator` returns 0 and either value is NaN, undefined, or null,
26042616
* that value will be returned.
26052617
*/
2606-
min(comparator?: (valueA: V, valueB: V) => number): V;
2618+
min(comparator?: (valueA: V, valueB: V) => number): V | undefined;
26072619

26082620
/**
26092621
* Like `min`, but also accepts a `comparatorValueMapper` which allows for
@@ -2615,7 +2627,7 @@
26152627
minBy<C>(
26162628
comparatorValueMapper: (value: V, key: K, iter: /*this*/Iterable<K, V>) => C,
26172629
comparator?: (valueA: C, valueB: C) => number
2618-
): V;
2630+
): V | undefined;
26192631

26202632

26212633
// Comparison
@@ -2721,7 +2733,7 @@
27212733
* @ignore
27222734
*/
27232735
export interface Iterator<T> {
2724-
next(): { value: T; done: boolean; }
2736+
next(): { value: T; done: boolean; } | undefined
27252737
}
27262738

27272739

dist/immutable.d.ts

+18-18
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,7 @@ declare module Immutable {
11621162
/**
11631163
* Alias for `Stack.first()`.
11641164
*/
1165-
peek(): T;
1165+
peek(): T | undefined;
11661166

11671167

11681168
// Persistent changes
@@ -1703,7 +1703,7 @@ declare module Immutable {
17031703
* `index` may be a negative number, which indexes back from the end of the
17041704
* Iterable. `s.get(-1)` gets the last item in the Iterable.
17051705
*/
1706-
get(index: number, notSetValue?: T): T;
1706+
get(index: number, notSetValue?: T): T | undefined;
17071707

17081708

17091709
// Conversion to Seq
@@ -1940,7 +1940,7 @@ declare module Immutable {
19401940
* so if `notSetValue` is not provided and this method returns `undefined`,
19411941
* that does not guarantee the key was not found.
19421942
*/
1943-
get(key: K, notSetValue?: V): V;
1943+
get(key: K, notSetValue?: V): V | undefined;
19441944

19451945
/**
19461946
* True if a key exists within this `Iterable`, using `Immutable.is` to determine equality
@@ -1957,12 +1957,12 @@ declare module Immutable {
19571957
/**
19581958
* The first value in the Iterable.
19591959
*/
1960-
first(): V;
1960+
first(): V | undefined;
19611961

19621962
/**
19631963
* The last value in the Iterable.
19641964
*/
1965-
last(): V;
1965+
last(): V | undefined;
19661966

19671967

19681968
// Reading deep values
@@ -2509,7 +2509,7 @@ declare module Immutable {
25092509
predicate: (value: V, key: K, iter: /*this*/Iterable<K, V>) => boolean,
25102510
context?: any,
25112511
notSetValue?: V
2512-
): V;
2512+
): V | undefined;
25132513

25142514
/**
25152515
* Returns the last value for which the `predicate` returns true.
@@ -2520,7 +2520,7 @@ declare module Immutable {
25202520
predicate: (value: V, key: K, iter: /*this*/Iterable<K, V>) => boolean,
25212521
context?: any,
25222522
notSetValue?: V
2523-
): V;
2523+
): V | undefined;
25242524

25252525
/**
25262526
* Returns the first [key, value] entry for which the `predicate` returns true.
@@ -2529,7 +2529,7 @@ declare module Immutable {
25292529
predicate: (value: V, key: K, iter: /*this*/Iterable<K, V>) => boolean,
25302530
context?: any,
25312531
notSetValue?: V
2532-
): /*[K, V]*/Array<any>;
2532+
): /*[K, V]*/Array<any> | undefined;
25332533

25342534
/**
25352535
* Returns the last [key, value] entry for which the `predicate`
@@ -2541,15 +2541,15 @@ declare module Immutable {
25412541
predicate: (value: V, key: K, iter: /*this*/Iterable<K, V>) => boolean,
25422542
context?: any,
25432543
notSetValue?: V
2544-
): /*[K, V]*/Array<any>;
2544+
): /*[K, V]*/Array<any> | undefined;
25452545

25462546
/**
25472547
* Returns the key for which the `predicate` returns true.
25482548
*/
25492549
findKey(
25502550
predicate: (value: V, key: K, iter: /*this*/Iterable.Keyed<K, V>) => boolean,
25512551
context?: any
2552-
): K;
2552+
): K | undefined;
25532553

25542554
/**
25552555
* Returns the last key for which the `predicate` returns true.
@@ -2559,17 +2559,17 @@ declare module Immutable {
25592559
findLastKey(
25602560
predicate: (value: V, key: K, iter: /*this*/Iterable.Keyed<K, V>) => boolean,
25612561
context?: any
2562-
): K;
2562+
): K | undefined;
25632563

25642564
/**
25652565
* Returns the key associated with the search value, or undefined.
25662566
*/
2567-
keyOf(searchValue: V): K;
2567+
keyOf(searchValue: V): K | undefined;
25682568

25692569
/**
25702570
* Returns the last key associated with the search value, or undefined.
25712571
*/
2572-
lastKeyOf(searchValue: V): K;
2572+
lastKeyOf(searchValue: V): K | undefined;
25732573

25742574
/**
25752575
* Returns the maximum value in this collection. If any values are
@@ -2586,7 +2586,7 @@ declare module Immutable {
25862586
* If `comparator` returns 0 and either value is NaN, undefined, or null,
25872587
* that value will be returned.
25882588
*/
2589-
max(comparator?: (valueA: V, valueB: V) => number): V;
2589+
max(comparator?: (valueA: V, valueB: V) => number): V | undefined;
25902590

25912591
/**
25922592
* Like `max`, but also accepts a `comparatorValueMapper` which allows for
@@ -2598,7 +2598,7 @@ declare module Immutable {
25982598
maxBy<C>(
25992599
comparatorValueMapper: (value: V, key: K, iter: /*this*/Iterable<K, V>) => C,
26002600
comparator?: (valueA: C, valueB: C) => number
2601-
): V;
2601+
): V | undefined;
26022602

26032603
/**
26042604
* Returns the minimum value in this collection. If any values are
@@ -2615,7 +2615,7 @@ declare module Immutable {
26152615
* If `comparator` returns 0 and either value is NaN, undefined, or null,
26162616
* that value will be returned.
26172617
*/
2618-
min(comparator?: (valueA: V, valueB: V) => number): V;
2618+
min(comparator?: (valueA: V, valueB: V) => number): V | undefined;
26192619

26202620
/**
26212621
* Like `min`, but also accepts a `comparatorValueMapper` which allows for
@@ -2627,7 +2627,7 @@ declare module Immutable {
26272627
minBy<C>(
26282628
comparatorValueMapper: (value: V, key: K, iter: /*this*/Iterable<K, V>) => C,
26292629
comparator?: (valueA: C, valueB: C) => number
2630-
): V;
2630+
): V | undefined;
26312631

26322632

26332633
// Comparison
@@ -2733,7 +2733,7 @@ declare module Immutable {
27332733
* @ignore
27342734
*/
27352735
export interface Iterator<T> {
2736-
next(): { value: T; done: boolean; }
2736+
next(): { value: T; done: boolean; } | undefined
27372737
}
27382738

27392739
}

0 commit comments

Comments
 (0)