|
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.6"; |
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.6"; |
| 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<[K, V]>; |
| 45 | + forEach(action: (value: V, key: K) => void): void; |
| 46 | +} |
39 | 47 |
|
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<[K, V]>; |
45 |
| - forEach(action: (value: V, key: K) => void): void; |
46 |
| - } |
| 48 | +/** |
| 49 | + * ES6 Map interface, only read methods included. |
| 50 | + */ |
| 51 | +export interface ReadonlyMap<T> extends ReadonlyESMap<string, T> { |
| 52 | +} |
47 | 53 |
|
48 |
| - /** |
49 |
| - * ES6 Map interface, only read methods included. |
50 |
| - */ |
51 |
| - export interface ReadonlyMap<T> extends ReadonlyESMap<string, T> { |
52 |
| - } |
| 54 | +/** ES6 Map interface. */ |
| 55 | +export interface ESMap<K, V> extends ReadonlyESMap<K, V>, Collection<K> { |
| 56 | + set(key: K, value: V): this; |
| 57 | +} |
53 | 58 |
|
54 |
| - /** ES6 Map interface. */ |
55 |
| - export interface ESMap<K, V> extends ReadonlyESMap<K, V>, Collection<K> { |
56 |
| - set(key: K, value: V): this; |
57 |
| - } |
| 59 | +/** |
| 60 | + * ES6 Map interface. |
| 61 | + */ |
| 62 | +export interface Map<T> extends ESMap<string, T> { |
| 63 | +} |
58 | 64 |
|
59 |
| - /** |
60 |
| - * ES6 Map interface. |
61 |
| - */ |
62 |
| - export interface Map<T> extends ESMap<string, T> { |
63 |
| - } |
| 65 | +/* @internal */ |
| 66 | +export interface MapConstructor { |
| 67 | + // eslint-disable-next-line @typescript-eslint/prefer-function-type |
| 68 | + new <K, V>(iterable?: readonly (readonly [K, V])[] | ReadonlyESMap<K, V>): ESMap<K, V>; |
| 69 | +} |
64 | 70 |
|
65 |
| - /* @internal */ |
66 |
| - export interface MapConstructor { |
67 |
| - // eslint-disable-next-line @typescript-eslint/prefer-function-type |
68 |
| - new <K, V>(iterable?: readonly (readonly [K, V])[] | ReadonlyESMap<K, V>): ESMap<K, V>; |
69 |
| - } |
| 71 | +/** ES6 Set interface, only read methods included. */ |
| 72 | +export interface ReadonlySet<T> extends ReadonlyCollection<T> { |
| 73 | + has(value: T): boolean; |
| 74 | + values(): Iterator<T>; |
| 75 | + entries(): Iterator<[T, T]>; |
| 76 | + forEach(action: (value: T, key: T) => void): void; |
| 77 | +} |
70 | 78 |
|
71 |
| - /** ES6 Set interface, only read methods included. */ |
72 |
| - export interface ReadonlySet<T> extends ReadonlyCollection<T> { |
73 |
| - has(value: T): boolean; |
74 |
| - values(): Iterator<T>; |
75 |
| - entries(): Iterator<[T, T]>; |
76 |
| - forEach(action: (value: T, key: T) => void): void; |
77 |
| - } |
| 79 | +/** ES6 Set interface. */ |
| 80 | +export interface Set<T> extends ReadonlySet<T>, Collection<T> { |
| 81 | + add(value: T): this; |
| 82 | + delete(value: T): boolean; |
| 83 | +} |
78 | 84 |
|
79 |
| - /** ES6 Set interface. */ |
80 |
| - export interface Set<T> extends ReadonlySet<T>, Collection<T> { |
81 |
| - add(value: T): this; |
82 |
| - delete(value: T): boolean; |
83 |
| - } |
| 85 | +/* @internal */ |
| 86 | +export interface SetConstructor { |
| 87 | + // eslint-disable-next-line @typescript-eslint/prefer-function-type |
| 88 | + new <T>(iterable?: readonly T[] | ReadonlySet<T>): Set<T>; |
| 89 | +} |
84 | 90 |
|
85 |
| - /* @internal */ |
86 |
| - export interface SetConstructor { |
87 |
| - // eslint-disable-next-line @typescript-eslint/prefer-function-type |
88 |
| - new <T>(iterable?: readonly T[] | ReadonlySet<T>): Set<T>; |
89 |
| - } |
| 91 | +/** ES6 Iterator type. */ |
| 92 | +export interface Iterator<T> { |
| 93 | + next(): { value: T, done?: false } | { value: void, done: true }; |
| 94 | +} |
90 | 95 |
|
91 |
| - /** ES6 Iterator type. */ |
92 |
| - export interface Iterator<T> { |
93 |
| - next(): { value: T, done?: false } | { value: void, done: true }; |
94 |
| - } |
| 96 | +/** Array that is only intended to be pushed to, never read. */ |
| 97 | +export interface Push<T> { |
| 98 | + push(...values: T[]): void; |
| 99 | + /* @internal*/ readonly length: number; |
| 100 | +} |
95 | 101 |
|
96 |
| - /** Array that is only intended to be pushed to, never read. */ |
97 |
| - export interface Push<T> { |
98 |
| - push(...values: T[]): void; |
99 |
| - /* @internal*/ readonly length: number; |
100 |
| - } |
| 102 | +/* @internal */ |
| 103 | +export type EqualityComparer<T> = (a: T, b: T) => boolean; |
101 | 104 |
|
102 |
| - /* @internal */ |
103 |
| - export type EqualityComparer<T> = (a: T, b: T) => boolean; |
| 105 | +/* @internal */ |
| 106 | +export type Comparer<T> = (a: T, b: T) => Comparison; |
104 | 107 |
|
105 |
| - /* @internal */ |
106 |
| - export type Comparer<T> = (a: T, b: T) => Comparison; |
| 108 | +/* @internal */ |
| 109 | +export const enum Comparison { |
| 110 | + LessThan = -1, |
| 111 | + EqualTo = 0, |
| 112 | + GreaterThan = 1 |
| 113 | +} |
107 | 114 |
|
108 |
| - /* @internal */ |
109 |
| - export const enum Comparison { |
110 |
| - LessThan = -1, |
111 |
| - EqualTo = 0, |
112 |
| - GreaterThan = 1 |
113 |
| - } |
| 115 | +/* @internal */ |
| 116 | +namespace NativeCollections { |
| 117 | + declare const Map: MapConstructor | undefined; |
| 118 | + declare const Set: SetConstructor | undefined; |
114 | 119 |
|
115 |
| - /* @internal */ |
116 |
| - namespace NativeCollections { |
117 |
| - declare const Map: MapConstructor | undefined; |
118 |
| - declare const Set: SetConstructor | undefined; |
119 |
| - |
120 |
| - /** |
121 |
| - * Returns the native Map implementation if it is available and compatible (i.e. supports iteration). |
122 |
| - */ |
123 |
| - export function tryGetNativeMap(): MapConstructor | undefined { |
124 |
| - // Internet Explorer's Map doesn't support iteration, so don't use it. |
125 |
| - // eslint-disable-next-line no-in-operator |
126 |
| - return typeof Map !== "undefined" && "entries" in Map.prototype && new Map([[0, 0]]).size === 1 ? Map : undefined; |
127 |
| - } |
128 |
| - |
129 |
| - /** |
130 |
| - * Returns the native Set implementation if it is available and compatible (i.e. supports iteration). |
131 |
| - */ |
132 |
| - export function tryGetNativeSet(): SetConstructor | undefined { |
133 |
| - // Internet Explorer's Set doesn't support iteration, so don't use it. |
134 |
| - // eslint-disable-next-line no-in-operator |
135 |
| - return typeof Set !== "undefined" && "entries" in Set.prototype && new Set([0]).size === 1 ? Set : undefined; |
136 |
| - } |
| 120 | + /** |
| 121 | + * Returns the native Map implementation if it is available and compatible (i.e. supports iteration). |
| 122 | + */ |
| 123 | + export function tryGetNativeMap(): MapConstructor | undefined { |
| 124 | + // Internet Explorer's Map doesn't support iteration, so don't use it. |
| 125 | + // eslint-disable-next-line no-in-operator |
| 126 | + return typeof Map !== "undefined" && "entries" in Map.prototype && new Map([[0, 0]]).size === 1 ? Map : undefined; |
137 | 127 | }
|
138 | 128 |
|
139 |
| - /* @internal */ |
140 |
| - export const Map = getCollectionImplementation("Map", "tryGetNativeMap", "createMapShim"); |
141 |
| - /* @internal */ |
142 |
| - export const Set = getCollectionImplementation("Set", "tryGetNativeSet", "createSetShim"); |
143 |
| - |
144 |
| - /* @internal */ |
145 |
| - type GetIteratorCallback = <I extends readonly any[] | ReadonlySet<any> | ReadonlyESMap<any, any> | undefined>(iterable: I) => Iterator< |
146 |
| - I extends ReadonlyESMap<infer K, infer V> ? [K, V] : |
147 |
| - I extends ReadonlySet<infer T> ? T : |
148 |
| - I extends readonly (infer T)[] ? T : |
149 |
| - I extends undefined ? undefined : |
150 |
| - never>; |
151 |
| - |
152 |
| - /* @internal */ |
153 |
| - function getCollectionImplementation< |
154 |
| - K1 extends MatchingKeys<typeof NativeCollections, () => any>, |
155 |
| - K2 extends MatchingKeys<typeof ShimCollections, (getIterator?: GetIteratorCallback) => ReturnType<(typeof NativeCollections)[K1]>> |
156 |
| - >(name: string, nativeFactory: K1, shimFactory: K2): NonNullable<ReturnType<(typeof NativeCollections)[K1]>> { |
157 |
| - // NOTE: ts.ShimCollections will be defined for typescriptServices.js but not for tsc.js, so we must test for it. |
158 |
| - const constructor = NativeCollections[nativeFactory]() ?? ShimCollections?.[shimFactory](getIterator); |
159 |
| - if (constructor) return constructor as NonNullable<ReturnType<(typeof NativeCollections)[K1]>>; |
160 |
| - throw new Error(`TypeScript requires an environment that provides a compatible native ${name} implementation.`); |
| 129 | + /** |
| 130 | + * Returns the native Set implementation if it is available and compatible (i.e. supports iteration). |
| 131 | + */ |
| 132 | + export function tryGetNativeSet(): SetConstructor | undefined { |
| 133 | + // Internet Explorer's Set doesn't support iteration, so don't use it. |
| 134 | + // eslint-disable-next-line no-in-operator |
| 135 | + return typeof Set !== "undefined" && "entries" in Set.prototype && new Set([0]).size === 1 ? Set : undefined; |
161 | 136 | }
|
162 | 137 | }
|
| 138 | + |
| 139 | +/* @internal */ |
| 140 | +export const Map = getCollectionImplementation("Map", "tryGetNativeMap", "createMapShim"); |
| 141 | +/* @internal */ |
| 142 | +export const Set = getCollectionImplementation("Set", "tryGetNativeSet", "createSetShim"); |
| 143 | + |
| 144 | +/* @internal */ |
| 145 | +type GetIteratorCallback = <I extends readonly any[] | ReadonlySet<any> | ReadonlyESMap<any, any> | undefined>(iterable: I) => Iterator< |
| 146 | + I extends ReadonlyESMap<infer K, infer V> ? [K, V] : |
| 147 | + I extends ReadonlySet<infer T> ? T : |
| 148 | + I extends readonly (infer T)[] ? T : |
| 149 | + I extends undefined ? undefined : |
| 150 | + never>; |
| 151 | + |
| 152 | +/* @internal */ |
| 153 | +function getCollectionImplementation< |
| 154 | + K1 extends MatchingKeys<typeof NativeCollections, () => any>, |
| 155 | + K2 extends MatchingKeys<typeof ShimCollections, (getIterator?: GetIteratorCallback) => ReturnType<(typeof NativeCollections)[K1]>> |
| 156 | +>(name: string, nativeFactory: K1, shimFactory: K2): NonNullable<ReturnType<(typeof NativeCollections)[K1]>> { |
| 157 | + // NOTE: ts.ShimCollections will be defined for typescriptServices.js but not for tsc.js, so we must test for it. |
| 158 | + const constructor = NativeCollections[nativeFactory]() ?? ShimCollections?.[shimFactory](getIterator); |
| 159 | + if (constructor) return constructor as NonNullable<ReturnType<(typeof NativeCollections)[K1]>>; |
| 160 | + throw new Error(`TypeScript requires an environment that provides a compatible native ${name} implementation.`); |
| 161 | +} |
| 162 | +} |
0 commit comments