Skip to content

Conversation

@Pimm
Copy link
Contributor

@Pimm Pimm commented Mar 26, 2021

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, as new Map<string> (note the sole type argument) does not compile anyway. If this is no good, please point me in the right direction.

JaredNeil and others added 4 commits March 26, 2021 17:42
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>.
@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Mar 26, 2021
@ghost
Copy link

ghost commented Mar 26, 2021

CLA assistant check
All CLA requirements met.

@Pimm Pimm force-pushed the map-constructor branch from 1855c29 to 755f187 Compare March 26, 2021 19:06
@Pimm
Copy link
Contributor Author

Pimm commented Mar 29, 2021

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 undefined and null lead to Map<unknown, unknown> seems consistent with non-strictNullCheck behaviour. However, perhaps Map<any, any> is preferred. What are your thoughts?

@sandersn sandersn self-requested a review April 5, 2021 22:00
@sandersn sandersn self-assigned this Apr 5, 2021
@sandersn sandersn requested a review from orta April 5, 2021 22:00
Copy link
Contributor

@orta orta left a 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;

@sandersn
Copy link
Member

@orta this looks ready to go. Were you holding it for 4.4?

@genshinw
Copy link

I have the same issue, Is this problem has resolve?

@Pimm
Copy link
Contributor Author

Pimm commented Jan 18, 2022

Until this lands in TypeScript, you can use ?? [] as a workaround. Like so:

const potentiallyUndefinedIterable = [['1', 1], ['2', 2]] as Iterable<[string, number]> | undefined;
new Map(potentiallyUndefinedIterable ?? []);

@orta
Copy link
Contributor

orta commented Jan 18, 2022

I think this is good to go 👍🏻

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

For Backlog Bug PRs that fix a backlog bug

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Type error for new Map from undefined Iterable

6 participants