|
1 | 1 | namespace ts { |
2 | | - // WARNING: The script `configurePrerelease.ts` uses a regexp to parse out these values. |
3 | | - // If changing the text in this section, be sure to test `configurePrerelease` too. |
4 | | - export const versionMajorMinor = "4.8"; |
5 | | - // The following is baselined as a literal template type without intervention |
6 | | - /** The version of the TypeScript compiler release */ |
7 | | - // eslint-disable-next-line @typescript-eslint/no-inferrable-types |
8 | | - export const version: string = `${versionMajorMinor}.0-dev`; |
| 2 | +// WARNING: The script `configurePrerelease.ts` uses a regexp to parse out these values. |
| 3 | +// If changing the text in this section, be sure to test `configurePrerelease` too. |
| 4 | +export const versionMajorMinor = "4.8"; |
| 5 | +// The following is baselined as a literal template type without intervention |
| 6 | +/** The version of the TypeScript compiler release */ |
| 7 | +// eslint-disable-next-line @typescript-eslint/no-inferrable-types |
| 8 | +export const version: string = `${versionMajorMinor}.0-dev`; |
| 9 | + |
| 10 | +/** |
| 11 | + * Type of objects whose values are all of the same type. |
| 12 | + * The `in` and `for-in` operators can *not* be safely used, |
| 13 | + * since `Object.prototype` may be modified by outside code. |
| 14 | + */ |
| 15 | +export interface MapLike<T> { |
| 16 | + [index: string]: T; |
| 17 | +} |
9 | 18 |
|
10 | | - /** |
11 | | - * Type of objects whose values are all of the same type. |
12 | | - * The `in` and `for-in` operators can *not* be safely used, |
13 | | - * since `Object.prototype` may be modified by outside code. |
14 | | - */ |
15 | | - export interface MapLike<T> { |
16 | | - [index: string]: T; |
17 | | - } |
| 19 | +export interface SortedReadonlyArray<T> extends ReadonlyArray<T> { |
| 20 | + " __sortedArrayBrand": any; |
| 21 | +} |
18 | 22 |
|
19 | | - export interface SortedReadonlyArray<T> extends ReadonlyArray<T> { |
20 | | - " __sortedArrayBrand": any; |
21 | | - } |
| 23 | +export interface SortedArray<T> extends Array<T> { |
| 24 | + " __sortedArrayBrand": any; |
| 25 | +} |
22 | 26 |
|
23 | | - export interface SortedArray<T> extends Array<T> { |
24 | | - " __sortedArrayBrand": any; |
25 | | - } |
| 27 | +/** Common read methods for ES6 Map/Set. */ |
| 28 | +export interface ReadonlyCollection<K> { |
| 29 | + readonly size: number; |
| 30 | + has(key: K): boolean; |
| 31 | + keys(): Iterator<K>; |
| 32 | +} |
26 | 33 |
|
27 | | - /** Common read methods for ES6 Map/Set. */ |
28 | | - export interface ReadonlyCollection<K> { |
29 | | - readonly size: number; |
30 | | - has(key: K): boolean; |
31 | | - keys(): Iterator<K>; |
32 | | - } |
| 34 | +/** Common write methods for ES6 Map/Set. */ |
| 35 | +export interface Collection<K> extends ReadonlyCollection<K> { |
| 36 | + delete(key: K): boolean; |
| 37 | + clear(): void; |
| 38 | +} |
33 | 39 |
|
34 | | - /** Common write methods for ES6 Map/Set. */ |
35 | | - export interface Collection<K> extends ReadonlyCollection<K> { |
36 | | - delete(key: K): boolean; |
37 | | - clear(): void; |
38 | | - } |
| 40 | +/** ES6 Map interface, only read methods included. */ |
| 41 | +export interface ReadonlyESMap<K, V> extends ReadonlyCollection<K> { |
| 42 | + get(key: K): V | undefined; |
| 43 | + values(): Iterator<V>; |
| 44 | + entries(): Iterator<[ |
| 45 | + K, |
| 46 | + V |
| 47 | + ]>; |
| 48 | + forEach(action: (value: V, key: K) => void): void; |
| 49 | +} |
39 | 50 |
|
40 | | - /** ES6 Map interface, only read methods included. */ |
41 | | - export interface ReadonlyESMap<K, V> extends ReadonlyCollection<K> { |
42 | | - get(key: K): V | undefined; |
43 | | - values(): Iterator<V>; |
44 | | - entries(): Iterator<[ |
45 | | - K, |
46 | | - V |
47 | | - ]>; |
48 | | - forEach(action: (value: V, key: K) => void): void; |
49 | | - } |
| 51 | +/** |
| 52 | + * ES6 Map interface, only read methods included. |
| 53 | + */ |
| 54 | +export interface ReadonlyMap<T> extends ReadonlyESMap<string, T> { |
| 55 | +} |
50 | 56 |
|
51 | | - /** |
52 | | - * ES6 Map interface, only read methods included. |
53 | | - */ |
54 | | - export interface ReadonlyMap<T> extends ReadonlyESMap<string, T> { |
55 | | - } |
| 57 | +/** ES6 Map interface. */ |
| 58 | +export interface ESMap<K, V> extends ReadonlyESMap<K, V>, Collection<K> { |
| 59 | + set(key: K, value: V): this; |
| 60 | +} |
56 | 61 |
|
57 | | - /** ES6 Map interface. */ |
58 | | - export interface ESMap<K, V> extends ReadonlyESMap<K, V>, Collection<K> { |
59 | | - set(key: K, value: V): this; |
60 | | - } |
| 62 | +/** |
| 63 | + * ES6 Map interface. |
| 64 | + */ |
| 65 | +export interface Map<T> extends ESMap<string, T> { |
| 66 | +} |
61 | 67 |
|
62 | | - /** |
63 | | - * ES6 Map interface. |
64 | | - */ |
65 | | - export interface Map<T> extends ESMap<string, T> { |
66 | | - } |
| 68 | +/* @internal */ |
| 69 | +export interface MapConstructor { |
| 70 | + // eslint-disable-next-line @typescript-eslint/prefer-function-type |
| 71 | + new <K, V>(iterable?: readonly (readonly [ |
| 72 | + K, |
| 73 | + V |
| 74 | + ])[] | ReadonlyESMap<K, V>): ESMap<K, V>; |
| 75 | +} |
67 | 76 |
|
68 | | - /* @internal */ |
69 | | - export interface MapConstructor { |
70 | | - // eslint-disable-next-line @typescript-eslint/prefer-function-type |
71 | | - new <K, V>(iterable?: readonly (readonly [ |
72 | | - K, |
73 | | - V |
74 | | - ])[] | ReadonlyESMap<K, V>): ESMap<K, V>; |
75 | | - } |
| 77 | +/** ES6 Set interface, only read methods included. */ |
| 78 | +export interface ReadonlySet<T> extends ReadonlyCollection<T> { |
| 79 | + has(value: T): boolean; |
| 80 | + values(): Iterator<T>; |
| 81 | + entries(): Iterator<[ |
| 82 | + T, |
| 83 | + T |
| 84 | + ]>; |
| 85 | + forEach(action: (value: T, key: T) => void): void; |
| 86 | +} |
76 | 87 |
|
77 | | - /** ES6 Set interface, only read methods included. */ |
78 | | - export interface ReadonlySet<T> extends ReadonlyCollection<T> { |
79 | | - has(value: T): boolean; |
80 | | - values(): Iterator<T>; |
81 | | - entries(): Iterator<[ |
82 | | - T, |
83 | | - T |
84 | | - ]>; |
85 | | - forEach(action: (value: T, key: T) => void): void; |
86 | | - } |
| 88 | +/** ES6 Set interface. */ |
| 89 | +export interface Set<T> extends ReadonlySet<T>, Collection<T> { |
| 90 | + add(value: T): this; |
| 91 | + delete(value: T): boolean; |
| 92 | +} |
87 | 93 |
|
88 | | - /** ES6 Set interface. */ |
89 | | - export interface Set<T> extends ReadonlySet<T>, Collection<T> { |
90 | | - add(value: T): this; |
91 | | - delete(value: T): boolean; |
92 | | - } |
| 94 | +/* @internal */ |
| 95 | +export interface SetConstructor { |
| 96 | + // eslint-disable-next-line @typescript-eslint/prefer-function-type |
| 97 | + new <T>(iterable?: readonly T[] | ReadonlySet<T>): Set<T>; |
| 98 | +} |
93 | 99 |
|
94 | | - /* @internal */ |
95 | | - export interface SetConstructor { |
96 | | - // eslint-disable-next-line @typescript-eslint/prefer-function-type |
97 | | - new <T>(iterable?: readonly T[] | ReadonlySet<T>): Set<T>; |
98 | | - } |
| 100 | +/** ES6 Iterator type. */ |
| 101 | +export interface Iterator<T> { |
| 102 | + next(): { |
| 103 | + value: T; |
| 104 | + done?: false; |
| 105 | + } | { |
| 106 | + value: void; |
| 107 | + done: true; |
| 108 | + }; |
| 109 | +} |
99 | 110 |
|
100 | | - /** ES6 Iterator type. */ |
101 | | - export interface Iterator<T> { |
102 | | - next(): { |
103 | | - value: T; |
104 | | - done?: false; |
105 | | - } | { |
106 | | - value: void; |
107 | | - done: true; |
108 | | - }; |
109 | | - } |
| 111 | +/** Array that is only intended to be pushed to, never read. */ |
| 112 | +export interface Push<T> { |
| 113 | + push(...values: T[]): void; |
| 114 | + /* @internal*/ readonly length: number; |
| 115 | +} |
110 | 116 |
|
111 | | - /** Array that is only intended to be pushed to, never read. */ |
112 | | - export interface Push<T> { |
113 | | - push(...values: T[]): void; |
114 | | - /* @internal*/ readonly length: number; |
115 | | - } |
| 117 | +/* @internal */ |
| 118 | +export type EqualityComparer<T> = (a: T, b: T) => boolean; |
116 | 119 |
|
117 | | - /* @internal */ |
118 | | - export type EqualityComparer<T> = (a: T, b: T) => boolean; |
| 120 | +/* @internal */ |
| 121 | +export type Comparer<T> = (a: T, b: T) => Comparison; |
119 | 122 |
|
120 | | - /* @internal */ |
121 | | - export type Comparer<T> = (a: T, b: T) => Comparison; |
| 123 | +/* @internal */ |
| 124 | +export const enum Comparison { |
| 125 | + LessThan = -1, |
| 126 | + EqualTo = 0, |
| 127 | + GreaterThan = 1 |
| 128 | +} |
122 | 129 |
|
123 | | - /* @internal */ |
124 | | - export const enum Comparison { |
125 | | - LessThan = -1, |
126 | | - EqualTo = 0, |
127 | | - GreaterThan = 1 |
128 | | - } |
| 130 | +/* @internal */ |
| 131 | +namespace NativeCollections { |
| 132 | + declare const self: any; |
129 | 133 |
|
130 | | - /* @internal */ |
131 | | - namespace NativeCollections { |
132 | | - declare const self: any; |
133 | | - |
134 | | - const globals = typeof globalThis !== "undefined" ? globalThis : |
135 | | - typeof global !== "undefined" ? global : |
136 | | - typeof self !== "undefined" ? self : |
137 | | - undefined; |
138 | | - |
139 | | - /** |
140 | | - * Returns the native Map implementation if it is available and compatible (i.e. supports iteration). |
141 | | - */ |
142 | | - export function tryGetNativeMap(): MapConstructor | undefined { |
143 | | - // Internet Explorer's Map doesn't support iteration, so don't use it. |
144 | | - const gMap = globals?.Map; |
145 | | - // eslint-disable-next-line no-in-operator |
146 | | - return typeof gMap !== "undefined" && "entries" in gMap.prototype && new gMap([[0, 0]]).size === 1 ? gMap : undefined; |
147 | | - } |
148 | | - |
149 | | - /** |
150 | | - * Returns the native Set implementation if it is available and compatible (i.e. supports iteration). |
151 | | - */ |
152 | | - export function tryGetNativeSet(): SetConstructor | undefined { |
153 | | - // Internet Explorer's Set doesn't support iteration, so don't use it. |
154 | | - const gSet = globals?.Set; |
155 | | - // eslint-disable-next-line no-in-operator |
156 | | - return typeof gSet !== "undefined" && "entries" in gSet.prototype && new gSet([0]).size === 1 ? gSet : undefined; |
157 | | - } |
158 | | - } |
| 134 | + const globals = typeof globalThis !== "undefined" ? globalThis : |
| 135 | + typeof global !== "undefined" ? global : |
| 136 | + typeof self !== "undefined" ? self : |
| 137 | + undefined; |
159 | 138 |
|
160 | | - /* @internal */ |
161 | | - export const Map = getCollectionImplementation("Map", "tryGetNativeMap", "createMapShim"); |
162 | | - /* @internal */ |
163 | | - export const Set = getCollectionImplementation("Set", "tryGetNativeSet", "createSetShim"); |
| 139 | + /** |
| 140 | + * Returns the native Map implementation if it is available and compatible (i.e. supports iteration). |
| 141 | + */ |
| 142 | + export function tryGetNativeMap(): MapConstructor | undefined { |
| 143 | + // Internet Explorer's Map doesn't support iteration, so don't use it. |
| 144 | + const gMap = globals?.Map; |
| 145 | + // eslint-disable-next-line no-in-operator |
| 146 | + return typeof gMap !== "undefined" && "entries" in gMap.prototype && new gMap([[0, 0]]).size === 1 ? gMap : undefined; |
| 147 | + } |
164 | 148 |
|
165 | | - /* @internal */ |
166 | | - type GetIteratorCallback = <I extends readonly any[] | ReadonlySet<any> | ReadonlyESMap<any, any> | undefined>(iterable: I) => Iterator<I extends ReadonlyESMap<infer K, infer V> ? [ |
167 | | - K, |
168 | | - V |
169 | | - ] : I extends ReadonlySet<infer T> ? T : I extends readonly (infer T)[] ? T : I extends undefined ? undefined : never>; |
170 | | - |
171 | | - /* @internal */ |
172 | | - function getCollectionImplementation<K1 extends ts.MatchingKeys<typeof NativeCollections, () => any>, K2 extends ts.MatchingKeys<typeof ts.ShimCollections, (getIterator?: GetIteratorCallback) => ReturnType<(typeof NativeCollections)[K1]>>>(name: string, nativeFactory: K1, shimFactory: K2): NonNullable<ReturnType<(typeof NativeCollections)[K1]>> { |
173 | | - // NOTE: ts.ShimCollections will be defined for typescriptServices.js but not for tsc.js, so we must test for it. |
174 | | - const constructor = NativeCollections[nativeFactory]() ?? ts.ShimCollections?.[shimFactory](ts.getIterator); |
175 | | - if (constructor) |
176 | | - return constructor as NonNullable<ReturnType<(typeof NativeCollections)[K1]>>; |
177 | | - throw new Error(`TypeScript requires an environment that provides a compatible native ${name} implementation.`); |
| 149 | + /** |
| 150 | + * Returns the native Set implementation if it is available and compatible (i.e. supports iteration). |
| 151 | + */ |
| 152 | + export function tryGetNativeSet(): SetConstructor | undefined { |
| 153 | + // Internet Explorer's Set doesn't support iteration, so don't use it. |
| 154 | + const gSet = globals?.Set; |
| 155 | + // eslint-disable-next-line no-in-operator |
| 156 | + return typeof gSet !== "undefined" && "entries" in gSet.prototype && new gSet([0]).size === 1 ? gSet : undefined; |
178 | 157 | } |
179 | 158 | } |
| 159 | + |
| 160 | +/* @internal */ |
| 161 | +export const Map = getCollectionImplementation("Map", "tryGetNativeMap", "createMapShim"); |
| 162 | +/* @internal */ |
| 163 | +export const Set = getCollectionImplementation("Set", "tryGetNativeSet", "createSetShim"); |
| 164 | + |
| 165 | +/* @internal */ |
| 166 | +type GetIteratorCallback = <I extends readonly any[] | ReadonlySet<any> | ReadonlyESMap<any, any> | undefined>(iterable: I) => Iterator<I extends ReadonlyESMap<infer K, infer V> ? [ |
| 167 | + K, |
| 168 | + V |
| 169 | +] : I extends ReadonlySet<infer T> ? T : I extends readonly (infer T)[] ? T : I extends undefined ? undefined : never>; |
| 170 | + |
| 171 | +/* @internal */ |
| 172 | +function getCollectionImplementation<K1 extends ts.MatchingKeys<typeof NativeCollections, () => any>, K2 extends ts.MatchingKeys<typeof ts.ShimCollections, (getIterator?: GetIteratorCallback) => ReturnType<(typeof NativeCollections)[K1]>>>(name: string, nativeFactory: K1, shimFactory: K2): NonNullable<ReturnType<(typeof NativeCollections)[K1]>> { |
| 173 | + // NOTE: ts.ShimCollections will be defined for typescriptServices.js but not for tsc.js, so we must test for it. |
| 174 | + const constructor = NativeCollections[nativeFactory]() ?? ts.ShimCollections?.[shimFactory](ts.getIterator); |
| 175 | + if (constructor) |
| 176 | + return constructor as NonNullable<ReturnType<(typeof NativeCollections)[K1]>>; |
| 177 | + throw new Error(`TypeScript requires an environment that provides a compatible native ${name} implementation.`); |
| 178 | +} |
| 179 | +} |
0 commit comments