-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Description
Suggestion
Description
const foo = new Map(); // The type is: Map<any, any>
const bar = new Set(); // The type is: Set<unknown>Here, the behavior of the types is inconsistent. Naively, I would expect both of them to be unknown, or both of them to be any, depending on what Microsoft wants to do. (Would it result in safer code for both of them to default to unknown?)
More Info
I asked about this inconsistency in the TypeScript discord:
Raziel — Today at 11:34 AM
Probably because nobody bothered updating the Map constructor type definition
226dd0b#diff-79b0ebcbd15b54ae283f4edf9b35237aed7957275439a4aeff66dde3a5258f48R160
mkantor — Today at 11:40 AM
i think it was actually this: #43396
mkantor — Today at 11:42 AM
tl;dr MapConstructor has this overload but SetConstructor doesn't have a similar one (not sure if there's a good reason why Map has that since its other constructor signature's parameter is optional anyway):TypeScript/src/lib/es2015.iterable.d.ts
Line 140 in 26eced3
new(): Map<any, any>;
jakub — Today at 1:59 PM
Is that overload even necessary?
it seems useless
🕗 Version & Regression Information
I tested on the playground, and:
- In v3.3.3, the default type of a Set is
any. - In v3.5.1 and later, the default type of a Set is
unknown, causing the inconsistency.