Skip to content

Commit

Permalink
Merge pull request #207 from amitdahan/fix-non-generic-map-constructor
Browse files Browse the repository at this point in the history
Fix `new Map()` always returning `Map<unknown, unknown>`
  • Loading branch information
mattpocock authored Sep 2, 2024
2 parents cfc8c1a + 922f8a8 commit b93fc1f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/entrypoints/map-constructor.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
interface MapConstructor {
new (): Map<unknown, unknown>;
new <K = unknown, V = unknown>(): Map<K, V>;
}
26 changes: 26 additions & 0 deletions src/tests/map-constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,29 @@ doNotExecute(() => {

type testClear = [Expect<Equal<typeof cleared, void>>];
});

doNotExecute(() => {
const map = new Map() satisfies Map<string, boolean>;
type test = [Expect<Equal<typeof map, Map<string, boolean>>>];
});

doNotExecute(() => {
const map: Map<string, boolean> = new Map();
type test = [Expect<Equal<typeof map, Map<string, boolean>>>];
});

doNotExecute(() => {
function expectsBooleanMap(map: Map<string, boolean>) {
return map;
}
const map = expectsBooleanMap(new Map());
type test = [Expect<Equal<typeof map, Map<string, boolean>>>];
});

doNotExecute(() => {
const map = new Map([
["foo", 1],
["bar", 2],
]);
type test = [Expect<Equal<typeof map, Map<string, number>>>];
});

0 comments on commit b93fc1f

Please sign in to comment.