-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Make Map constructor argument optional and nullable #43396
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
Conversation
According to the spec (https://tc39.es/ecma262/#sec-map-iterable), the sole argument passed to Map is allowed to be null or undefined.
This proves that the previous commit fixes microsoft#37779, as well as that new Map() still types as Map<any, any>.
|
With this patch applied and strictNullChecks on: const potentiallyUndefinedIterable = [['1', 1], ['2', 2]] as Iterable<[string, number]> | undefined;
new Map(potentiallyUndefinedIterable); // Map<string, number> ✔️new Map(undefined); // Map<unknown, unknown> ❓new Map(null); // Map<unknown, unknown> ❓This behaviour ‒ where |
orta
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, seems reasonable to me, this implementation also matches how we do it in lib.es2015.collection.d.ts
interface MapConstructor {
new(): Map<any, any>;
new<K, V>(entries?: readonly (readonly [K, V])[] | null): Map<K, V>;
readonly prototype: Map<any, any>;
}
declare var Map: MapConstructor;
|
@orta this looks ready to go. Were you holding it for 4.4? |
|
I have the same issue, Is this problem has resolve? |
|
Until this lands in TypeScript, you can use const potentiallyUndefinedIterable = [['1', 1], ['2', 2]] as Iterable<[string, number]> | undefined;
new Map(potentiallyUndefinedIterable ?? []); |
|
I think this is good to go 👍🏻 |
Fixes #37779.
This PR is based on #38153 by @JaredNeil.
As demonstrated by the change in the baseline file, this affects the typing of
new Map<string>. I don't quite understand why that is the case. I also don't know whether this is an issue, asnew Map<string>(note the sole type argument) does not compile anyway. If this is no good, please point me in the right direction.