Skip to content

Wrong typescript declarations for .update() method (continued) #1931

Closed
@cypherfunc

Description

@cypherfunc

What happened

This is a continuation of issue 1466.
In 1842, | undefined was added to the updater function's parameter type, but not the return type. As mentioned in a comment on that issue, the return type also needs to allow undefined. This is specifically needed for cases where no update is applied if the entry does not exist, or where the entry should be deleted.

e.g.

const myMap = Map<string, List<string>>();
const stillMyMap = myMap.update("noKey", ls => ls?.map(x => x + "blah"));

Since myMap doesn't have an entry for "noKey", ls is undefined, and thus the updater returns undefined.

How to reproduce

Run Typescript type-checking on the above example, with strictNullChecks turned on. You will get an error like:

error TS2345: Argument of type '(ls: List<string>|undefined) => List<string>|undefined' is not assignable to parameter of type  '(ls: List<string>|undefined) => List<string>'.
  Type 'List<string>|undefined' is not assignable to type 'List<string>'.
    Type 'undefined' is not assignable to type 'List<string>'.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions