Skip to content

Add generic type to collections getters #47235

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
Syjalo opened this issue Dec 24, 2021 · 1 comment
Closed

Add generic type to collections getters #47235

Syjalo opened this issue Dec 24, 2021 · 1 comment

Comments

@Syjalo
Copy link

Syjalo commented Dec 24, 2021

lib Update Request

Configuration Check

My compilation target is es2021 and my lib is es2021.

Missing / Incorrect Definition

A generic type in get method of collections.

Sample Code

const m = new Map<string, string | number>();
m.set('key', 'value');
conat v = m.get('key') // Map<string, string | number>.get(key: string): string | number | undefined

Request

const m = new Map<string, string | number>();
m.set('key', 'value');
conat v = m.get<string>('key') // Map<string, string | number>.get<string>(key: string): string | undefined

Documentation Link

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get

@MartinJohns
Copy link
Contributor

MartinJohns commented Dec 24, 2021

This would mistakingly lead people to believe they will surely get the specific type. Same reason why JSON.parse() is not generic. See this comment.

There's no reason to favour m.get<string>('key') over <string>m.get('key') (or m.get('key') as string).

@Syjalo Syjalo closed this as completed Dec 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants