Skip to content

Change Map default generic parameters from "any" to "unknown" #52552

@Zamiell

Description

@Zamiell

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):

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions