Skip to content

fix: map constructor param type - #64901

Open
Dave van Soest (tbknl) wants to merge 9 commits into
microsoft:mainfrom
tbknl:fix/mapconstructor-param-type
Open

Dave van Soest (tbknl) wants to merge 9 commits into
microsoft:mainfrom
tbknl:fix/mapconstructor-param-type

Conversation

@tbknl

@tbknl Dave van Soest (tbknl) commented Oct 21, 2026 •

Copy link
Copy Markdown

Fixes #37527
Fixes #33611
Fixes #39133

Additional notes

  • PR consists of 2 essential changes. The first commit adds test cases exposing the type bug, the second implements the fix for the the type inference. Additional commits address type inference regression and alignment between the collection and iterable libs.
    • -- update -- A 3rd commit is added, with only a scratch file.
    • -- update -- A 4th commit is added, deleting an accidental scratch file from the previous commit.
    • -- update -- A 5th commit is added, to align the inferred Map types between collection and iterable libs for empty- array constructor arguments.
    • -- update -- A 6th commit is added, to fix the overload ordering as recommended in the copilot comment.
    • -- update -- A 7th commit is added, to make the collection lib's overload generic constraint equal that of the iterable lib.
  • PR accepts changes in multiple baselines references, which previously included incorrectly inferred types.
  • The types for the following code are aligned between the collection and iterable libs, in case one is used without the other, they infer the same types:
    const a: any[] = []; // Same as `const a = [];`, but then without compiler errors.
    const m1 = new Map(a); // Infers `Map<any, any>`. (Formerly `Map<unknown, unknown>`.)
    const m2 = new Map([]); // Infers `Map<unknown, unknown>`. (Same as before.)
    The MapConstructor interface in the iterable lib is updated to align this behavior. Note that prior to this PR, the type of m1 in this example was inferred differently for both libs.

Copilot AI balanced review requested due to automatic review settings October 21, 2026 14:19
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Oct 21, 2026
@typescript-automation typescript-automation Bot added the For Backlog Bug PRs that fix a backlog bug label Oct 21, 2026
@tbknl Dave van Soest (tbknl) changed the title Fix/mapconstructor param type fix: map constructor param type Oct 21, 2026
@tbknl

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The overload ordering breaks empty-map insertion when only the collection library is included.

No open issues 🎉

What changed in this PR

Improves Map constructor inference for heterogeneous keys and values, addressing #37527, #33611, and #39133.

Changes:

  • Adds an overload that derives key and value types from entry arrays.
  • Adds regression cases and updates inferred-type and diagnostic baselines.
File Description
tsc/​testdata/​tests/​cases/​conformance/​es6/​mapConstruction/​new-Map1.ts Adds heterogeneous-entry tests.
tsc/​testdata/​baselines/​reference/​conformance/​new-Map1.types Records inferred map types.
tsc/​testdata/​baselines/​reference/​conformance/​new-Map1.symbols Records test symbols.
tsc/​testdata/​baselines/​reference/​conformance/​new-Map1.js Records emitted JavaScript.
tsc/​testdata/​baselines/​reference/​conformance/​iterableArrayPattern28.types Updates union value types.
tsc/​testdata/​baselines/​reference/​conformance/​iterableArrayPattern28.errors.txt Updates argument mismatch diagnostics.
tsc/​testdata/​baselines/​reference/​conformance/​for-of39.types Updates loop value inference.
tsc/​testdata/​baselines/​reference/​conformance/​for-of39.errors.txt Removes obsolete constructor errors.
tsc/​testdata/​baselines/​reference/​compiler/​newMap.errors.txt Updates type-argument diagnostics.
tsc/​internal/​bundled/​libs/​lib.es2015.collection.d.ts Adds entry-array inference overload.

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The temporary file blocks library generation, and the overload regresses empty-input and contextual inference with collection-only libraries.

1 open issue

Review effort: Balanced

Comment thread tsc/internal/bundled/libs/lib.es2015.collection.temp.ts Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Empty-array construction regresses in collection-only configurations, rejecting previously valid insertions.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Collection-only empty-array inference regresses to Map<never, never>, rejecting previously valid insertions.

No open issues 🎉

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The new overloads assign different meanings to a single explicit type argument, making constructor calls depend on the selected libraries.

No open issues 🎉

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Global standard-library overload changes warrant human review because collection-only inference lacks regression coverage and full-suite validation remains unverified.

No open issues 🎉

Review effort: Balanced

@tbknl

Copy link
Copy Markdown
Author

🔵 Needs a closer look

Global standard-library overload changes warrant human review because collection-only inference lacks regression coverage and full-suite validation remains unverified.

Specific library-only test coverage (e.g. to test only with collection lib without the iterable lib) does not appear to exist in this repo.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Backlog Bug PRs that fix a backlog bug

Projects

Status: Not started

2 participants