Skip to content

Wrong typescript declarations for update method #1466

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

Closed
JLarky opened this issue Dec 23, 2017 · 6 comments · Fixed by #1842
Closed

Wrong typescript declarations for update method #1466

JLarky opened this issue Dec 23, 2017 · 6 comments · Fixed by #1842

Comments

@JLarky
Copy link

JLarky commented Dec 23, 2017

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.

@JLarky
Copy link
Author

JLarky commented Dec 23, 2017

I also noticed that you can return undefined from updater and it will delete the key from map, so I guess it's a updater: (value: V | undefined) => V | undefined)

@remi-blaise
Copy link

@JLarky In Typescript undefined extends any type, so the updater type is updater: (value?: V) => V) which is fine.

@JLarky
Copy link
Author

JLarky commented Dec 31, 2017 via email

@remi-blaise
Copy link

Does Immutable support --strict flag ?

@JLarky
Copy link
Author

JLarky commented Jan 2, 2018 via email

@JLarky
Copy link
Author

JLarky commented Feb 15, 2018

Noticed similar issue with mapEntries method. It allows you to return undefined instead of tuple to remove entry from collection so

   mapEntries<KM, VM>(
      mapper: (entry: [K, V], index: number, iter: this) => [KM, VM],
      context?: any
    ): Map<KM, VM>;

should be

   mapEntries<KM, VM>(
      mapper: (entry: [K, V], index: number, iter: this) => [KM, VM] | undefined,
      context?: any
    ): Map<KM, VM>;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants