Skip to content

TS: update method may return undefined #1933

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 1 commit into from
Feb 9, 2023
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
11 changes: 7 additions & 4 deletions type-definitions/immutable.d.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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