Skip to content

Commit c1beaa1

Browse files
committed
Fix detecting an existing Map/Set
This didn't affect compilation to CJS since that sets `exports.Map` instead of creating a global.
1 parent 04d77fe commit c1beaa1

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/compiler/corePublic.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,22 +117,30 @@ namespace ts {
117117
declare const Map: MapConstructor | undefined;
118118
declare const Set: SetConstructor | undefined;
119119

120+
const globals = typeof globalThis !== "undefined" ? globalThis
121+
: typeof global !== "undefined" ? global
122+
// @ts-ignore
123+
: typeof window !== "undefined" ? window
124+
: undefined;
125+
120126
/**
121127
* Returns the native Map implementation if it is available and compatible (i.e. supports iteration).
122128
*/
123129
export function tryGetNativeMap(): MapConstructor | undefined {
124130
// Internet Explorer's Map doesn't support iteration, so don't use it.
131+
const gMap = globals.Map;
125132
// eslint-disable-next-line no-in-operator
126-
return typeof Map !== "undefined" && "entries" in Map.prototype && new Map([[0, 0]]).size === 1 ? Map : undefined;
133+
return typeof gMap !== "undefined" && "entries" in gMap.prototype && new gMap([[0, 0]]).size === 1 ? gMap : undefined;
127134
}
128135

129136
/**
130137
* Returns the native Set implementation if it is available and compatible (i.e. supports iteration).
131138
*/
132139
export function tryGetNativeSet(): SetConstructor | undefined {
133140
// Internet Explorer's Set doesn't support iteration, so don't use it.
141+
const gSet = globals.Set;
134142
// eslint-disable-next-line no-in-operator
135-
return typeof Set !== "undefined" && "entries" in Set.prototype && new Set([0]).size === 1 ? Set : undefined;
143+
return typeof gSet !== "undefined" && "entries" in gSet.prototype && new gSet([0]).size === 1 ? gSet : undefined;
136144
}
137145
}
138146

0 commit comments

Comments
 (0)