Skip to content

Commit

Permalink
implement .update thanks to @mbullington
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeniau committed Nov 12, 2021
1 parent 0d57333 commit 4674d2d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
8 changes: 8 additions & 0 deletions type-definitions/immutable.d.ts
Expand Up @@ -793,6 +793,14 @@ declare namespace Immutable {

set<K extends keyof R>(key: K, value: R[K]): this;

update(updater: (value: this) => this): this;
update<K extends keyof R>(key: K, updater: (value: R[K]) => R[K]): this;
update<K extends keyof R, NSV>(
key: K,
notSetValue: NSV,
updater: (value: R[K]) => NSV
): this;

// Possible best type is MapFromObject<Omit<R, K>> but Omit seems to broke other function calls
// and generate recursion error with other methods (update, merge, etc.) until those functions are defined in MapFromObject
delete<K extends keyof R>(
Expand Down
17 changes: 16 additions & 1 deletion type-definitions/ts-tests/map.ts
Expand Up @@ -250,7 +250,22 @@ import { Map, List, MapFromObject } from 'immutable';
Map<number, number>().update(1, 10, (v: number | undefined) => v + 'a');

// $ExpectError
Map({ a: 1, b: 'b' }).update('c', (v: any) => v);
Map({ a: 1, b: 'b' }).update('c', (v) => v);

// $ExpectType MapFromObject<{ a: number; b: string; }>
Map({ a: 1, b: 'b' }).update('b', (v) => v.toUpperCase());

// $ExpectType MapFromObject<{ a: number; b: string; }>
Map({ a: 1, b: 'b' }).update('b', 'NSV', (v) => v.toUpperCase());

// $ExpectError
Map({ a: 1, b: 'b' }).update((v) => ({ a: 'a' }));

// $ExpectType MapFromObject<{ a: number; b: string; }>
Map({ a: 1, b: 'b' }).update((v) => v.set('a', 2).set('b', 'B'));

// $ExpectError
Map({ a: 1, b: 'b' }).update((v) => v.set('c', 'c'));
}

{
Expand Down

0 comments on commit 4674d2d

Please sign in to comment.