Skip to content

Commit 6927730

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 b9efc3b commit 6927730

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/compiler/corePublic.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -114,25 +114,31 @@ namespace ts {
114114

115115
/* @internal */
116116
namespace NativeCollections {
117-
declare const Map: MapConstructor | undefined;
118-
declare const Set: SetConstructor | undefined;
117+
declare const self: any;
118+
119+
const globals = typeof globalThis !== "undefined" ? globalThis :
120+
typeof global !== "undefined" ? global :
121+
typeof self !== "undefined" ? self :
122+
undefined;
119123

120124
/**
121125
* Returns the native Map implementation if it is available and compatible (i.e. supports iteration).
122126
*/
123127
export function tryGetNativeMap(): MapConstructor | undefined {
124128
// Internet Explorer's Map doesn't support iteration, so don't use it.
129+
const gMap = globals?.Map;
125130
// eslint-disable-next-line no-in-operator
126-
return typeof Map !== "undefined" && "entries" in Map.prototype && new Map([[0, 0]]).size === 1 ? Map : undefined;
131+
return typeof gMap !== "undefined" && "entries" in gMap.prototype && new gMap([[0, 0]]).size === 1 ? gMap : undefined;
127132
}
128133

129134
/**
130135
* Returns the native Set implementation if it is available and compatible (i.e. supports iteration).
131136
*/
132137
export function tryGetNativeSet(): SetConstructor | undefined {
133138
// Internet Explorer's Set doesn't support iteration, so don't use it.
139+
const gSet = globals?.Set;
134140
// eslint-disable-next-line no-in-operator
135-
return typeof Set !== "undefined" && "entries" in Set.prototype && new Set([0]).size === 1 ? Set : undefined;
141+
return typeof gSet !== "undefined" && "entries" in gSet.prototype && new gSet([0]).size === 1 ? gSet : undefined;
136142
}
137143
}
138144

0 commit comments

Comments
 (0)