@@ -124,45 +124,34 @@ namespace ts {
124
124
/**
125
125
* Returns the native Map implementation if it is available and compatible (i.e. supports iteration).
126
126
*/
127
- export function tryGetNativeMap ( ) : MapConstructor | undefined {
127
+ export function tryGetNativeMap ( ) : MapConstructor {
128
128
// Internet Explorer's Map doesn't support iteration, so don't use it.
129
129
const gMap = globals ?. Map ;
130
130
// eslint-disable-next-line no-in-operator
131
- return typeof gMap !== "undefined" && "entries" in gMap . prototype && new gMap ( [ [ 0 , 0 ] ] ) . size === 1 ? gMap : undefined ;
131
+ const constructor = typeof gMap !== "undefined" && "entries" in gMap . prototype && new gMap ( [ [ 0 , 0 ] ] ) . size === 1 ? gMap : undefined ;
132
+ if ( ! constructor ) {
133
+ throw new Error ( "No compatible Map implementation found." ) ;
134
+ }
135
+ return constructor ;
132
136
}
133
137
134
138
/**
135
139
* Returns the native Set implementation if it is available and compatible (i.e. supports iteration).
136
140
*/
137
- export function tryGetNativeSet ( ) : SetConstructor | undefined {
141
+ export function tryGetNativeSet ( ) : SetConstructor {
138
142
// Internet Explorer's Set doesn't support iteration, so don't use it.
139
143
const gSet = globals ?. Set ;
140
144
// eslint-disable-next-line no-in-operator
141
- return typeof gSet !== "undefined" && "entries" in gSet . prototype && new gSet ( [ 0 ] ) . size === 1 ? gSet : undefined ;
145
+ const constructor = typeof gSet !== "undefined" && "entries" in gSet . prototype && new gSet ( [ 0 ] ) . size === 1 ? gSet : undefined ;
146
+ if ( ! constructor ) {
147
+ throw new Error ( "No compatible Set implementation found." ) ;
148
+ }
149
+ return constructor ;
142
150
}
143
151
}
144
152
145
153
/* @internal */
146
- export const Map = getCollectionImplementation ( "Map" , " tryGetNativeMap" , "createMapShim" ) ;
154
+ export const Map = NativeCollections . tryGetNativeMap ( ) ;
147
155
/* @internal */
148
- export const Set = getCollectionImplementation ( "Set" , "tryGetNativeSet" , "createSetShim" ) ;
149
-
150
- /* @internal */
151
- type GetIteratorCallback = < I extends readonly any [ ] | ReadonlySet < any > | ReadonlyESMap < any , any > | undefined > ( iterable : I ) => Iterator <
152
- I extends ReadonlyESMap < infer K , infer V > ? [ K , V ] :
153
- I extends ReadonlySet < infer T > ? T :
154
- I extends readonly ( infer T ) [ ] ? T :
155
- I extends undefined ? undefined :
156
- never > ;
157
-
158
- /* @internal */
159
- function getCollectionImplementation <
160
- K1 extends MatchingKeys < typeof NativeCollections , ( ) => any > ,
161
- K2 extends MatchingKeys < typeof ShimCollections , ( getIterator ?: GetIteratorCallback ) => ReturnType < ( typeof NativeCollections ) [ K1 ] > >
162
- > ( name : string , nativeFactory : K1 , shimFactory : K2 ) : NonNullable < ReturnType < ( typeof NativeCollections ) [ K1 ] > > {
163
- // NOTE: ts.ShimCollections will be defined for typescriptServices.js but not for tsc.js, so we must test for it.
164
- const constructor = NativeCollections [ nativeFactory ] ( ) ?? ShimCollections ?. [ shimFactory ] ( getIterator ) ;
165
- if ( constructor ) return constructor as NonNullable < ReturnType < ( typeof NativeCollections ) [ K1 ] > > ;
166
- throw new Error ( `TypeScript requires an environment that provides a compatible native ${ name } implementation.` ) ;
167
- }
156
+ export const Set = NativeCollections . tryGetNativeSet ( ) ;
168
157
}
0 commit comments