Skip to content

Commit

Permalink
TS: update method may return undefined (#1933)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeniau committed Feb 9, 2023
1 parent af0a387 commit f88e4f9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
11 changes: 7 additions & 4 deletions type-definitions/immutable.d.ts
Expand Up @@ -424,7 +424,10 @@ declare namespace Immutable {
* @see `Map#update`
*/
update(index: number, notSetValue: T, updater: (value: T) => T): this;
update(index: number, updater: (value: T | undefined) => T): this;
update(
index: number,
updater: (value: T | undefined) => T | undefined
): this;
update<R>(updater: (value: this) => R): R;

/**
Expand Down Expand Up @@ -1001,7 +1004,7 @@ declare namespace Immutable {
* Note: `update(key)` can be used in `withMutations`.
*/
update(key: K, notSetValue: V, updater: (value: V) => V): this;
update(key: K, updater: (value: V | undefined) => V): this;
update(key: K, updater: (value: V | undefined) => V | undefined): this;
update<R>(updater: (value: this) => R): R;

/**
Expand Down Expand Up @@ -5571,7 +5574,7 @@ declare namespace Immutable {
function update<K, V, C extends Collection<K, V>>(
collection: C,
key: K,
updater: (value: V | undefined) => V
updater: (value: V | undefined) => V | undefined
): C;
function update<K, V, C extends Collection<K, V>, NSV>(
collection: C,
Expand All @@ -5598,7 +5601,7 @@ declare namespace Immutable {
function update<V>(
collection: Array<V>,
key: number,
updater: (value: V) => V
updater: (value: V | undefined) => V | undefined
): Array<V>;
function update<V, NSV>(
collection: Array<V>,
Expand Down
3 changes: 3 additions & 0 deletions type-definitions/ts-tests/list.ts
Expand Up @@ -265,6 +265,9 @@ import {
// $ExpectError
List<number>().update(1, 10, (v: number | undefined) => v + 'a');

// $ExpectType List<string>
List<string>().update(1, (v) => v?.toUpperCase());

// $ExpectType List<number>
update(List<number>(), 0, (v: number | undefined) => 0);

Expand Down
3 changes: 3 additions & 0 deletions type-definitions/ts-tests/map.ts
Expand Up @@ -176,6 +176,9 @@ import { Map, List } from 'immutable';

// $ExpectError
Map<number, number>().update(1, 10, (v: number | undefined) => v + 'a');

// $ExpectType Map<string, string>
Map<string, string>().update("noKey", ls => ls?.toUpperCase());
}

{
Expand Down

0 comments on commit f88e4f9

Please sign in to comment.