Skip to content

Wrong typescript declarations for update method #1466

Closed
@JLarky

Description

@JLarky

What happened

update(key: K, updater: (value: V) => V): this;

here updater has wrong type, should be updater: (value?: V) => V) if key doesn't exist

How to reproduce

const good = I.Map<string, I.List<string>>().update("noKey", I.List(), a => a.map(x => x));
const bad = I.Map<string, I.List<string>>().update("noKey", a => a.map(x => x));
const fixed = I.Map<string, I.List<string>>().update("noKey", undefined as any, (a: undefined | I.List<string>) => a.map(x => x));

image

Here good works as expected, fixed shows error message that I would expect to see in this case Object is possibly 'undefined', but bad doesn't show any errors while crashing at run time with Uncaught TypeError: Cannot read property 'map' of undefined

Maybe there need to be a function that is updateIfKeyExist that would have (value: V) => V type, but right now type is misleading and causes run time issues.

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