From 8d6b844b321b7a4c67c6bdb110299666b88ac5a1 Mon Sep 17 00:00:00 2001 From: graphemecluster Date: Sun, 29 May 2022 21:12:45 +0800 Subject: [PATCH 1/2] Improve inference for tuple types --- docs/diff/es2015.core.d.ts.md | 15 +- docs/diff/es5.d.ts.md | 409 ++++++++++++++++++++++++++------- generated/lib.es2015.core.d.ts | 3 +- generated/lib.es5.d.ts | 272 ++++++++++------------ lib/lib.es2015.core.d.ts | 3 +- lib/lib.es5.d.ts | 272 ++++++++++------------ lib/lib.util.d.ts | 62 +++++ tests/src/es5.ts | 266 ++++++++++++++++++++- 8 files changed, 901 insertions(+), 401 deletions(-) diff --git a/docs/diff/es2015.core.d.ts.md b/docs/diff/es2015.core.d.ts.md index 0552ea2..212a007 100644 --- a/docs/diff/es2015.core.d.ts.md +++ b/docs/diff/es2015.core.d.ts.md @@ -147,28 +147,15 @@ Index: es2015.core.d.ts * the empty string is returned. * @param count number of copies to append */ -@@ -466,10 +436,10 @@ - startsWith(searchString: string, position?: number): boolean; +@@ -467,9 +437,8 @@ /** * Returns an `` HTML anchor element and sets the name attribute to the text value -+ * @param name * @deprecated A legacy feature for browser compatibility - * @param name */ anchor(name: string): string; /** -@@ -514,9 +484,9 @@ - */ - fontsize(size: string): string; - - /** -- * Returns an `` HTML element -+ * Returns a `` HTML element - * @deprecated A legacy feature for browser compatibility - */ - italics(): string; - ``` diff --git a/docs/diff/es5.d.ts.md b/docs/diff/es5.d.ts.md index 19ae5b6..334cf50 100644 --- a/docs/diff/es5.d.ts.md +++ b/docs/diff/es5.d.ts.md @@ -5,7 +5,7 @@ Index: es5.d.ts =================================================================== --- es5.d.ts +++ es5.d.ts -@@ -1,4 +1,23 @@ +@@ -1,4 +1,85 @@ +/// + +type First = T extends [any] ? T[0] : unknown; @@ -16,6 +16,68 @@ Index: es5.d.ts + ? F + : unknown; + ++/* ++type Item = number extends A["length"] ++ ? T | undefined ++ : A["length"] extends 0 ++ ? undefined ++ : T; ++*/ ++ ++type ReverseArray = T extends [ ++ infer F, ++ ...infer R ++] ++ ? ReverseArray ++ : U; ++ ++type Reverse = number extends A["length"] ++ ? T[] ++ : ReverseArray; ++ ++type RangeArray = A["length"] extends N ++ ? A ++ : RangeArray; ++ ++type Indices = number extends A["length"] ++ ? number ++ : RangeArray extends (infer A)[] ++ ? A ++ : never; ++ ++type FilterArray< ++ T extends readonly unknown[], ++ S, ++ U extends unknown[] ++> = T extends readonly [infer F, ...infer R] ++ ? FilterArray extends S ? [...U, First] : U> ++ : U; ++ ++type Filter = number extends T["length"] ++ ? S[] ++ : FilterArray<{ [K in keyof T]: [T[K]] }, S, []>; ++ ++type Cast = T extends U ? T : U; ++ ++type ValueIndexPairArray< ++ T extends readonly unknown[], ++ A extends unknown[] = [] ++> = A["length"] extends T["length"] ++ ? A ++ : ValueIndexPairArray< ++ T, ++ [...A, [value: T[A["length"]], index: A["length"], array: T]] ++ >; ++ ++type ValueIndexPair = Cast< ++ number extends A["length"] ++ ? unknown ++ : ValueIndexPairArray extends (infer R)[] ++ ? R ++ : unknown, ++ [value: T, index: number, array: A] ++>; ++ +type JSONValue = + | null + | string @@ -29,7 +91,7 @@ Index: es5.d.ts /// ECMAScript APIs ///////////////////////////// -@@ -8,9 +27,9 @@ +@@ -8,9 +89,9 @@ /** * Evaluates JavaScript code and executes it. * @param x A String value that contains valid JavaScript code. @@ -40,7 +102,7 @@ Index: es5.d.ts /** * Converts a string to an integer. * @param string A string to convert into a number. -@@ -117,9 +136,19 @@ +@@ -117,9 +198,19 @@ /** * Determines whether an object has a property with the specified name. * @param v A property name. @@ -61,7 +123,7 @@ Index: es5.d.ts /** * Determines whether an object exists in another object's prototype chain. * @param v Another object whose prototype chain is to be checked. -@@ -132,21 +161,26 @@ +@@ -132,21 +223,26 @@ */ propertyIsEnumerable(v: PropertyKey): boolean; } @@ -91,7 +153,7 @@ Index: es5.d.ts /** * Gets the own property descriptor of the specified object. * An own property descriptor is one that is defined directly on the object and is not inherited from the object's prototype. -@@ -162,47 +196,101 @@ +@@ -162,47 +258,101 @@ * Returns the names of the own properties of an object. The own properties of an object are those that are defined directly * on that object, and are not inherited from the object's prototype. The properties of an object include both fields (objects) and functions. * @param o Object that contains the own properties. @@ -208,7 +270,7 @@ Index: es5.d.ts /** * Prevents the modification of attributes of existing properties, and prevents the addition of new properties. * @param o Object on which to lock the attributes. -@@ -210,17 +298,17 @@ +@@ -210,17 +360,17 @@ seal(o: T): T; /** @@ -230,7 +292,7 @@ Index: es5.d.ts /** * Prevents the modification of existing property attributes and values, and prevents the addition of new properties. * @param o Object on which to lock the attributes. -@@ -258,13 +346,8 @@ +@@ -258,13 +408,8 @@ keys(o: object): string[]; } @@ -244,7 +306,7 @@ Index: es5.d.ts */ interface Function { /** -@@ -331,11 +414,16 @@ +@@ -331,11 +476,16 @@ interface CallableFunction extends Function { /** * Calls the function with the specified object as the this value and the elements of specified array as the arguments. @@ -262,7 +324,7 @@ Index: es5.d.ts this: (this: T, ...args: A) => R, thisArg: T, args: A -@@ -357,49 +445,27 @@ +@@ -357,49 +507,27 @@ * The this object of the bound function is associated with the specified object, and has the specified initial parameters. * @param thisArg The object to be used as the this object. * @param args Arguments to bind to the parameters of the function. @@ -322,7 +384,7 @@ Index: es5.d.ts this: new (...args: A) => T, thisArg: T, args: A -@@ -421,44 +487,17 @@ +@@ -421,44 +549,17 @@ * The this object of the bound function is associated with the specified object, and has the specified initial parameters. * @param thisArg The object to be used as the this object. * @param args Arguments to bind to the parameters of the function. @@ -372,7 +434,7 @@ Index: es5.d.ts callee: Function; } -@@ -511,21 +550,26 @@ +@@ -511,21 +612,26 @@ match(regexp: string | RegExp): RegExpMatchArray | null; /** @@ -402,7 +464,7 @@ Index: es5.d.ts /** * Finds the first substring match in a regular expression search. -@@ -586,8 +630,24 @@ +@@ -586,8 +692,24 @@ /** Returns the primitive value of the specified object. */ valueOf(): string; @@ -427,7 +489,7 @@ Index: es5.d.ts interface StringConstructor { new (value?: any): String; -@@ -595,13 +655,8 @@ +@@ -595,13 +717,8 @@ readonly prototype: String; fromCharCode(...codes: number[]): string; } @@ -441,7 +503,7 @@ Index: es5.d.ts /** Returns the primitive value of the specified object. */ valueOf(): boolean; } -@@ -1187,43 +1242,81 @@ +@@ -1187,43 +1304,81 @@ * If a member contains nested objects, the nested objects are transformed before the parent object is. */ parse( @@ -536,7 +598,24 @@ Index: es5.d.ts /** * Gets the length of the array. This is a number one higher than the highest element defined in an array. */ -@@ -1276,11 +1369,16 @@ +@@ -1261,38 +1416,43 @@ + * Returns the index of the first occurrence of a value in an array. + * @param searchElement The value to locate in the array. + * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0. + */ +- indexOf(searchElement: T, fromIndex?: number): number; ++ indexOf(searchElement: T, fromIndex?: number): -1 | Indices; + /** + * Returns the index of the last occurrence of a specified value in an array. + * @param searchElement The value to locate in the array. + * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at the last index in the array. + */ +- lastIndexOf(searchElement: T, fromIndex?: number): number; ++ lastIndexOf(searchElement: T, fromIndex?: number): -1 | Indices; + /** + * Determines whether all the members of an array satisfy the specified test. + * @param predicate A function that accepts up to three arguments. The every method calls + * the predicate function for each element in the array until the predicate returns a value * which is coercible to the Boolean value false, or until the end of the array. * @param thisArg An object to which the this keyword can refer in the predicate function. * If thisArg is omitted, undefined is used as the this value. @@ -544,19 +623,20 @@ Index: es5.d.ts - every( - predicate: (value: T, index: number, array: readonly T[]) => value is S, - thisArg?: any +- ): this is readonly S[]; + every( + predicate: ( + this: This, + value: T, -+ index: number, -+ array: readonly T[] ++ index: Indices, ++ array: this + ) => value is S, + thisArg?: This - ): this is readonly S[]; ++ ): this is { [K in keyof this]: S }; /** * Determines whether all the members of an array satisfy the specified test. * @param predicate A function that accepts up to three arguments. The every method calls -@@ -1288,11 +1386,16 @@ + * the predicate function for each element in the array until the predicate returns a value * which is coercible to the Boolean value false, or until the end of the array. * @param thisArg An object to which the this keyword can refer in the predicate function. * If thisArg is omitted, undefined is used as the this value. @@ -565,18 +645,13 @@ Index: es5.d.ts - predicate: (value: T, index: number, array: readonly T[]) => unknown, - thisArg?: any + every( -+ predicate: ( -+ this: This, -+ value: T, -+ index: number, -+ array: readonly T[] -+ ) => boolean, ++ predicate: (this: This, ...args: ValueIndexPair) => boolean, + thisArg?: This ): boolean; /** * Determines whether the specified callback function returns true for any element of an array. * @param predicate A function that accepts up to three arguments. The some method calls -@@ -1300,47 +1403,67 @@ +@@ -1300,118 +1460,85 @@ * which is coercible to the Boolean value true, or until the end of the array. * @param thisArg An object to which the this keyword can refer in the predicate function. * If thisArg is omitted, undefined is used as the this value. @@ -585,12 +660,7 @@ Index: es5.d.ts - predicate: (value: T, index: number, array: readonly T[]) => unknown, - thisArg?: any + some( -+ predicate: ( -+ this: This, -+ value: T, -+ index: number, -+ array: readonly T[] -+ ) => boolean, ++ predicate: (this: This, ...args: ValueIndexPair) => boolean, + thisArg?: This ): boolean; /** @@ -602,12 +672,7 @@ Index: es5.d.ts - callbackfn: (value: T, index: number, array: readonly T[]) => void, - thisArg?: any + forEach( -+ callbackfn: ( -+ this: This, -+ value: T, -+ index: number, -+ array: readonly T[] -+ ) => void, ++ callbackfn: (this: This, ...args: ValueIndexPair) => void, + thisArg?: This ): void; /** @@ -618,10 +683,11 @@ Index: es5.d.ts - map( - callbackfn: (value: T, index: number, array: readonly T[]) => U, - thisArg?: any +- ): U[]; + map( -+ callbackfn: (this: This, value: T, index: number, array: readonly T[]) => U, ++ callbackfn: (this: This, ...args: ValueIndexPair) => U, + thisArg?: This - ): U[]; ++ ): { -readonly [K in keyof this]: U }; /** * Returns the elements of an array that meet the condition specified in a callback function. * @param predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array. @@ -630,15 +696,16 @@ Index: es5.d.ts - filter( - predicate: (value: T, index: number, array: readonly T[]) => value is S, - thisArg?: any +- ): S[]; + filter( + predicate: ( + this: This, + value: T, -+ index: number, -+ array: readonly T[] ++ index: Indices, ++ array: this + ) => value is S, + thisArg?: This - ): S[]; ++ ): Filter; /** * Returns the elements of an array that meet the condition specified in a callback function. * @param predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array. @@ -648,38 +715,135 @@ Index: es5.d.ts - predicate: (value: T, index: number, array: readonly T[]) => unknown, - thisArg?: any + filter( -+ predicate: ( -+ this: This, -+ value: T, -+ index: number, -+ array: readonly T[] -+ ) => boolean, ++ predicate: (this: This, ...args: ValueIndexPair) => boolean, + thisArg?: This ): T[]; /** * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array. -@@ -1500,17 +1623,17 @@ - * @param start The zero-based location in the array from which to start removing elements. - * @param deleteCount The number of elements to remove. - * @returns An array containing the elements that were deleted. + * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. + */ +- reduce( +- callbackfn: ( +- previousValue: T, +- currentValue: T, +- currentIndex: number, +- array: readonly T[] +- ) => T +- ): T; +- reduce( +- callbackfn: ( +- previousValue: T, +- currentValue: T, +- currentIndex: number, +- array: readonly T[] +- ) => T, +- initialValue: T +- ): T; ++ reduce( ++ callbackfn: (previousValue: T | U, ...args: ValueIndexPair) => U ++ ): U; + /** + * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. + * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array. + * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. + */ +- reduce( +- callbackfn: ( +- previousValue: U, +- currentValue: T, +- currentIndex: number, +- array: readonly T[] +- ) => U, ++ reduce( ++ callbackfn: (previousValue: U, ...args: ValueIndexPair) => U, + initialValue: U + ): U; + /** + * Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. + * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array. + * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. + */ +- reduceRight( +- callbackfn: ( +- previousValue: T, +- currentValue: T, +- currentIndex: number, +- array: readonly T[] +- ) => T +- ): T; +- reduceRight( +- callbackfn: ( +- previousValue: T, +- currentValue: T, +- currentIndex: number, +- array: readonly T[] +- ) => T, +- initialValue: T +- ): T; ++ reduceRight( ++ callbackfn: (previousValue: T | U, ...args: ValueIndexPair) => U ++ ): U; + /** + * Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. + * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array. + * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. + */ +- reduceRight( +- callbackfn: ( +- previousValue: U, +- currentValue: T, +- currentIndex: number, +- array: readonly T[] +- ) => U, ++ reduceRight( ++ callbackfn: (previousValue: U, ...args: ValueIndexPair) => U, + initialValue: U + ): U; + + readonly [n: number]: T; +@@ -1467,9 +1594,9 @@ + /** + * Reverses the elements in an array in place. + * This method mutates the array and returns a reference to the same array. + */ +- reverse(): T[]; ++ reverse(): Reverse; + /** + * Removes the first element from an array and returns it. + * If the array is empty, undefined is returned and the array is not modified. + */ +@@ -1493,9 +1620,9 @@ + * ```ts + * [11,2,22,1].sort((a, b) => a - b) + * ``` */ -- splice(start: number, deleteCount?: number): T[]; -+ splice(start: number, deleteCount?: number): this; +- sort(compareFn?: (a: T, b: T) => number): this; ++ sort(compareFn?: (a: T, b: T) => number): T[]; /** * Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements. * @param start The zero-based location in the array from which to start removing elements. * @param deleteCount The number of elements to remove. - * @param items Elements to insert into the array in place of the deleted elements. - * @returns An array containing the elements that were deleted. +@@ -1517,40 +1644,43 @@ + unshift(...items: T[]): number; + /** + * Returns the index of the first occurrence of a value in an array, or -1 if it is not present. + * @param searchElement The value to locate in the array. +- * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0. */ -- splice(start: number, deleteCount: number, ...items: T[]): T[]; -+ splice(start: number, deleteCount: number, ...items: T[]): this; +- indexOf(searchElement: T, fromIndex?: number): number; ++ indexOf(searchElement: T, fromIndex?: number): -1 | Indices; /** - * Inserts new elements at the start of an array, and returns the new length of the array. - * @param items Elements to insert at the start of the array. + * Returns the index of the last occurrence of a specified value in an array, or -1 if it is not present. + * @param searchElement The value to locate in the array. +- * @param fromIndex The array index at which to begin searching backward. If fromIndex is omitted, the search starts at the last index in the array. */ -@@ -1534,11 +1657,11 @@ +- lastIndexOf(searchElement: T, fromIndex?: number): number; ++ lastIndexOf(searchElement: T, fromIndex?: number): -1 | Indices; + /** + * Determines whether all the members of an array satisfy the specified test. + * @param predicate A function that accepts up to three arguments. The every method calls + * the predicate function for each element in the array until the predicate returns a value * which is coercible to the Boolean value false, or until the end of the array. * @param thisArg An object to which the this keyword can refer in the predicate function. * If thisArg is omitted, undefined is used as the this value. @@ -687,14 +851,20 @@ Index: es5.d.ts - every( - predicate: (value: T, index: number, array: T[]) => value is S, - thisArg?: any +- ): this is S[]; + every( -+ predicate: (this: This, value: T, index: number, array: T[]) => value is S, ++ predicate: ( ++ this: This, ++ value: T, ++ index: Indices, ++ array: this ++ ) => value is S, + thisArg?: This - ): this is S[]; ++ ): this is { [K in keyof this]: S }; /** * Determines whether all the members of an array satisfy the specified test. * @param predicate A function that accepts up to three arguments. The every method calls -@@ -1546,11 +1669,11 @@ + * the predicate function for each element in the array until the predicate returns a value * which is coercible to the Boolean value false, or until the end of the array. * @param thisArg An object to which the this keyword can refer in the predicate function. * If thisArg is omitted, undefined is used as the this value. @@ -703,13 +873,13 @@ Index: es5.d.ts - predicate: (value: T, index: number, array: T[]) => unknown, - thisArg?: any + every( -+ predicate: (this: This, value: T, index: number, array: T[]) => boolean, ++ predicate: (this: This, ...args: ValueIndexPair) => boolean, + thisArg?: This ): boolean; /** * Determines whether the specified callback function returns true for any element of an array. * @param predicate A function that accepts up to three arguments. The some method calls -@@ -1558,47 +1681,47 @@ +@@ -1558,137 +1688,100 @@ * which is coercible to the Boolean value true, or until the end of the array. * @param thisArg An object to which the this keyword can refer in the predicate function. * If thisArg is omitted, undefined is used as the this value. @@ -718,7 +888,7 @@ Index: es5.d.ts - predicate: (value: T, index: number, array: T[]) => unknown, - thisArg?: any + some( -+ predicate: (this: This, value: T, index: number, array: T[]) => boolean, ++ predicate: (this: This, ...args: ValueIndexPair) => boolean, + thisArg?: This ): boolean; /** @@ -730,7 +900,7 @@ Index: es5.d.ts - callbackfn: (value: T, index: number, array: T[]) => void, - thisArg?: any + forEach( -+ callbackfn: (this: This, value: T, index: number, array: T[]) => void, ++ callbackfn: (this: This, ...args: ValueIndexPair) => void, + thisArg?: This ): void; /** @@ -741,10 +911,11 @@ Index: es5.d.ts - map( - callbackfn: (value: T, index: number, array: T[]) => U, - thisArg?: any +- ): U[]; + map( -+ callbackfn: (this: This, value: T, index: number, array: T[]) => U, ++ callbackfn: (this: This, ...args: ValueIndexPair) => U, + thisArg?: This - ): U[]; ++ ): { [K in keyof this]: U }; /** * Returns the elements of an array that meet the condition specified in a callback function. * @param predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array. @@ -753,10 +924,16 @@ Index: es5.d.ts - filter( - predicate: (value: T, index: number, array: T[]) => value is S, - thisArg?: any +- ): S[]; + filter( -+ predicate: (this: This, value: T, index: number, array: T[]) => value is S, ++ predicate: ( ++ this: This, ++ value: T, ++ index: Indices, ++ array: this ++ ) => value is S, + thisArg?: This - ): S[]; ++ ): Filter; /** * Returns the elements of an array that meet the condition specified in a callback function. * @param predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array. @@ -766,13 +943,91 @@ Index: es5.d.ts - predicate: (value: T, index: number, array: T[]) => unknown, - thisArg?: any + filter( -+ predicate: (this: This, value: T, index: number, array: T[]) => boolean, ++ predicate: (this: This, ...args: ValueIndexPair) => boolean, + thisArg?: This ): T[]; /** * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array. -@@ -1674,21 +1797,19 @@ +- * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. + */ +- reduce( +- callbackfn: ( +- previousValue: T, +- currentValue: T, +- currentIndex: number, +- array: T[] +- ) => T +- ): T; +- reduce( +- callbackfn: ( +- previousValue: T, +- currentValue: T, +- currentIndex: number, +- array: T[] +- ) => T, +- initialValue: T +- ): T; ++ reduce( ++ callbackfn: (previousValue: T | U, ...args: ValueIndexPair) => U ++ ): U; + /** + * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. + * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array. + * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. + */ +- reduce( +- callbackfn: ( +- previousValue: U, +- currentValue: T, +- currentIndex: number, +- array: T[] +- ) => U, ++ reduce( ++ callbackfn: (previousValue: U, ...args: ValueIndexPair) => U, + initialValue: U + ): U; + /** + * Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. + * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array. +- * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. + */ +- reduceRight( +- callbackfn: ( +- previousValue: T, +- currentValue: T, +- currentIndex: number, +- array: T[] +- ) => T +- ): T; +- reduceRight( +- callbackfn: ( +- previousValue: T, +- currentValue: T, +- currentIndex: number, +- array: T[] +- ) => T, +- initialValue: T +- ): T; ++ reduceRight( ++ callbackfn: (previousValue: T | U, ...args: ValueIndexPair) => U ++ ): U; + /** + * Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. + * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array. + * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. + */ +- reduceRight( +- callbackfn: ( +- previousValue: U, +- currentValue: T, +- currentIndex: number, +- array: T[] +- ) => U, ++ reduceRight( ++ callbackfn: (previousValue: U, ...args: ValueIndexPair) => U, + initialValue: U + ): U; [n: number]: T; } @@ -798,7 +1053,7 @@ Index: es5.d.ts enumerable?: boolean; configurable?: boolean; writable?: boolean; -@@ -1716,9 +1837,15 @@ +@@ -1716,9 +1809,15 @@ ) => void; declare type PromiseConstructorLike = new ( @@ -815,7 +1070,7 @@ Index: es5.d.ts ) => void ) => PromiseLike; -@@ -5435,22 +5562,8 @@ +@@ -5435,22 +5534,8 @@ ): string[]; }; } diff --git a/generated/lib.es2015.core.d.ts b/generated/lib.es2015.core.d.ts index a0e0df8..3b802e5 100644 --- a/generated/lib.es2015.core.d.ts +++ b/generated/lib.es2015.core.d.ts @@ -568,7 +568,6 @@ interface String { /** * Returns an `` HTML anchor element and sets the name attribute to the text value - * @param name * @deprecated A legacy feature for browser compatibility */ anchor(name: string): string; @@ -616,7 +615,7 @@ interface String { fontsize(size: string): string; /** - * Returns a `` HTML element + * Returns an `` HTML element * @deprecated A legacy feature for browser compatibility */ italics(): string; diff --git a/generated/lib.es5.d.ts b/generated/lib.es5.d.ts index 6185ab1..c2f0e62 100644 --- a/generated/lib.es5.d.ts +++ b/generated/lib.es5.d.ts @@ -8,6 +8,68 @@ type UnionToIntersection = ( ? F : unknown; +/* +type Item = number extends A["length"] + ? T | undefined + : A["length"] extends 0 + ? undefined + : T; +*/ + +type ReverseArray = T extends [ + infer F, + ...infer R +] + ? ReverseArray + : U; + +type Reverse = number extends A["length"] + ? T[] + : ReverseArray; + +type RangeArray = A["length"] extends N + ? A + : RangeArray; + +type Indices = number extends A["length"] + ? number + : RangeArray extends (infer A)[] + ? A + : never; + +type FilterArray< + T extends readonly unknown[], + S, + U extends unknown[] +> = T extends readonly [infer F, ...infer R] + ? FilterArray extends S ? [...U, First] : U> + : U; + +type Filter = number extends T["length"] + ? S[] + : FilterArray<{ [K in keyof T]: [T[K]] }, S, []>; + +type Cast = T extends U ? T : U; + +type ValueIndexPairArray< + T extends readonly unknown[], + A extends unknown[] = [] +> = A["length"] extends T["length"] + ? A + : ValueIndexPairArray< + T, + [...A, [value: T[A["length"]], index: A["length"], array: T]] + >; + +type ValueIndexPair = Cast< + number extends A["length"] + ? unknown + : ValueIndexPairArray extends (infer R)[] + ? R + : unknown, + [value: T, index: number, array: A] +>; + type JSONValue = | null | string @@ -1747,13 +1809,13 @@ interface ReadonlyArray { * @param searchElement The value to locate in the array. * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0. */ - indexOf(searchElement: T, fromIndex?: number): number; + indexOf(searchElement: T, fromIndex?: number): -1 | Indices; /** * Returns the index of the last occurrence of a specified value in an array. * @param searchElement The value to locate in the array. * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at the last index in the array. */ - lastIndexOf(searchElement: T, fromIndex?: number): number; + lastIndexOf(searchElement: T, fromIndex?: number): -1 | Indices; /** * Determines whether all the members of an array satisfy the specified test. * @param predicate A function that accepts up to three arguments. The every method calls @@ -1766,11 +1828,11 @@ interface ReadonlyArray { predicate: ( this: This, value: T, - index: number, - array: readonly T[] + index: Indices, + array: this ) => value is S, thisArg?: This - ): this is readonly S[]; + ): this is { [K in keyof this]: S }; /** * Determines whether all the members of an array satisfy the specified test. * @param predicate A function that accepts up to three arguments. The every method calls @@ -1780,12 +1842,7 @@ interface ReadonlyArray { * If thisArg is omitted, undefined is used as the this value. */ every( - predicate: ( - this: This, - value: T, - index: number, - array: readonly T[] - ) => boolean, + predicate: (this: This, ...args: ValueIndexPair) => boolean, thisArg?: This ): boolean; /** @@ -1797,12 +1854,7 @@ interface ReadonlyArray { * If thisArg is omitted, undefined is used as the this value. */ some( - predicate: ( - this: This, - value: T, - index: number, - array: readonly T[] - ) => boolean, + predicate: (this: This, ...args: ValueIndexPair) => boolean, thisArg?: This ): boolean; /** @@ -1811,12 +1863,7 @@ interface ReadonlyArray { * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. */ forEach( - callbackfn: ( - this: This, - value: T, - index: number, - array: readonly T[] - ) => void, + callbackfn: (this: This, ...args: ValueIndexPair) => void, thisArg?: This ): void; /** @@ -1825,9 +1872,9 @@ interface ReadonlyArray { * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. */ map( - callbackfn: (this: This, value: T, index: number, array: readonly T[]) => U, + callbackfn: (this: This, ...args: ValueIndexPair) => U, thisArg?: This - ): U[]; + ): { -readonly [K in keyof this]: U }; /** * Returns the elements of an array that meet the condition specified in a callback function. * @param predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array. @@ -1837,23 +1884,18 @@ interface ReadonlyArray { predicate: ( this: This, value: T, - index: number, - array: readonly T[] + index: Indices, + array: this ) => value is S, thisArg?: This - ): S[]; + ): Filter; /** * Returns the elements of an array that meet the condition specified in a callback function. * @param predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array. * @param thisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value. */ filter( - predicate: ( - this: This, - value: T, - index: number, - array: readonly T[] - ) => boolean, + predicate: (this: This, ...args: ValueIndexPair) => boolean, thisArg?: This ): T[]; /** @@ -1861,35 +1903,16 @@ interface ReadonlyArray { * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array. * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. */ - reduce( - callbackfn: ( - previousValue: T, - currentValue: T, - currentIndex: number, - array: readonly T[] - ) => T - ): T; - reduce( - callbackfn: ( - previousValue: T, - currentValue: T, - currentIndex: number, - array: readonly T[] - ) => T, - initialValue: T - ): T; + reduce( + callbackfn: (previousValue: T | U, ...args: ValueIndexPair) => U + ): U; /** * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array. * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. */ - reduce( - callbackfn: ( - previousValue: U, - currentValue: T, - currentIndex: number, - array: readonly T[] - ) => U, + reduce( + callbackfn: (previousValue: U, ...args: ValueIndexPair) => U, initialValue: U ): U; /** @@ -1897,35 +1920,16 @@ interface ReadonlyArray { * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array. * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. */ - reduceRight( - callbackfn: ( - previousValue: T, - currentValue: T, - currentIndex: number, - array: readonly T[] - ) => T - ): T; - reduceRight( - callbackfn: ( - previousValue: T, - currentValue: T, - currentIndex: number, - array: readonly T[] - ) => T, - initialValue: T - ): T; + reduceRight( + callbackfn: (previousValue: T | U, ...args: ValueIndexPair) => U + ): U; /** * Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array. * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. */ - reduceRight( - callbackfn: ( - previousValue: U, - currentValue: T, - currentIndex: number, - array: readonly T[] - ) => U, + reduceRight( + callbackfn: (previousValue: U, ...args: ValueIndexPair) => U, initialValue: U ): U; @@ -2116,7 +2120,7 @@ interface Array { * Reverses the elements in an array in place. * This method mutates the array and returns a reference to the same array. */ - reverse(): T[]; + reverse(): Reverse; /** * Removes the first element from an array and returns it. * If the array is empty, undefined is returned and the array is not modified. @@ -2142,14 +2146,14 @@ interface Array { * [11,2,22,1].sort((a, b) => a - b) * ``` */ - sort(compareFn?: (a: T, b: T) => number): this; + sort(compareFn?: (a: T, b: T) => number): T[]; /** * Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements. * @param start The zero-based location in the array from which to start removing elements. * @param deleteCount The number of elements to remove. * @returns An array containing the elements that were deleted. */ - splice(start: number, deleteCount?: number): this; + splice(start: number, deleteCount?: number): T[]; /** * Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements. * @param start The zero-based location in the array from which to start removing elements. @@ -2157,7 +2161,7 @@ interface Array { * @param items Elements to insert into the array in place of the deleted elements. * @returns An array containing the elements that were deleted. */ - splice(start: number, deleteCount: number, ...items: T[]): this; + splice(start: number, deleteCount: number, ...items: T[]): T[]; /** * Inserts new elements at the start of an array, and returns the new length of the array. * @param items Elements to insert at the start of the array. @@ -2166,15 +2170,13 @@ interface Array { /** * Returns the index of the first occurrence of a value in an array, or -1 if it is not present. * @param searchElement The value to locate in the array. - * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0. */ - indexOf(searchElement: T, fromIndex?: number): number; + indexOf(searchElement: T, fromIndex?: number): -1 | Indices; /** * Returns the index of the last occurrence of a specified value in an array, or -1 if it is not present. * @param searchElement The value to locate in the array. - * @param fromIndex The array index at which to begin searching backward. If fromIndex is omitted, the search starts at the last index in the array. */ - lastIndexOf(searchElement: T, fromIndex?: number): number; + lastIndexOf(searchElement: T, fromIndex?: number): -1 | Indices; /** * Determines whether all the members of an array satisfy the specified test. * @param predicate A function that accepts up to three arguments. The every method calls @@ -2184,9 +2186,14 @@ interface Array { * If thisArg is omitted, undefined is used as the this value. */ every( - predicate: (this: This, value: T, index: number, array: T[]) => value is S, + predicate: ( + this: This, + value: T, + index: Indices, + array: this + ) => value is S, thisArg?: This - ): this is S[]; + ): this is { [K in keyof this]: S }; /** * Determines whether all the members of an array satisfy the specified test. * @param predicate A function that accepts up to three arguments. The every method calls @@ -2196,7 +2203,7 @@ interface Array { * If thisArg is omitted, undefined is used as the this value. */ every( - predicate: (this: This, value: T, index: number, array: T[]) => boolean, + predicate: (this: This, ...args: ValueIndexPair) => boolean, thisArg?: This ): boolean; /** @@ -2208,7 +2215,7 @@ interface Array { * If thisArg is omitted, undefined is used as the this value. */ some( - predicate: (this: This, value: T, index: number, array: T[]) => boolean, + predicate: (this: This, ...args: ValueIndexPair) => boolean, thisArg?: This ): boolean; /** @@ -2217,7 +2224,7 @@ interface Array { * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. */ forEach( - callbackfn: (this: This, value: T, index: number, array: T[]) => void, + callbackfn: (this: This, ...args: ValueIndexPair) => void, thisArg?: This ): void; /** @@ -2226,97 +2233,62 @@ interface Array { * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. */ map( - callbackfn: (this: This, value: T, index: number, array: T[]) => U, + callbackfn: (this: This, ...args: ValueIndexPair) => U, thisArg?: This - ): U[]; + ): { [K in keyof this]: U }; /** * Returns the elements of an array that meet the condition specified in a callback function. * @param predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array. * @param thisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value. */ filter( - predicate: (this: This, value: T, index: number, array: T[]) => value is S, + predicate: ( + this: This, + value: T, + index: Indices, + array: this + ) => value is S, thisArg?: This - ): S[]; + ): Filter; /** * Returns the elements of an array that meet the condition specified in a callback function. * @param predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array. * @param thisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value. */ filter( - predicate: (this: This, value: T, index: number, array: T[]) => boolean, + predicate: (this: This, ...args: ValueIndexPair) => boolean, thisArg?: This ): T[]; /** * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array. - * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. */ - reduce( - callbackfn: ( - previousValue: T, - currentValue: T, - currentIndex: number, - array: T[] - ) => T - ): T; - reduce( - callbackfn: ( - previousValue: T, - currentValue: T, - currentIndex: number, - array: T[] - ) => T, - initialValue: T - ): T; + reduce( + callbackfn: (previousValue: T | U, ...args: ValueIndexPair) => U + ): U; /** * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array. * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. */ - reduce( - callbackfn: ( - previousValue: U, - currentValue: T, - currentIndex: number, - array: T[] - ) => U, + reduce( + callbackfn: (previousValue: U, ...args: ValueIndexPair) => U, initialValue: U ): U; /** * Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array. - * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. */ - reduceRight( - callbackfn: ( - previousValue: T, - currentValue: T, - currentIndex: number, - array: T[] - ) => T - ): T; - reduceRight( - callbackfn: ( - previousValue: T, - currentValue: T, - currentIndex: number, - array: T[] - ) => T, - initialValue: T - ): T; + reduceRight( + callbackfn: (previousValue: T | U, ...args: ValueIndexPair) => U + ): U; /** * Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array. * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. */ - reduceRight( - callbackfn: ( - previousValue: U, - currentValue: T, - currentIndex: number, - array: T[] - ) => U, + reduceRight( + callbackfn: (previousValue: U, ...args: ValueIndexPair) => U, initialValue: U ): U; diff --git a/lib/lib.es2015.core.d.ts b/lib/lib.es2015.core.d.ts index 8417d4a..8bb67c4 100644 --- a/lib/lib.es2015.core.d.ts +++ b/lib/lib.es2015.core.d.ts @@ -162,7 +162,6 @@ interface String { /** * Returns an `` HTML anchor element and sets the name attribute to the text value - * @param name * @deprecated A legacy feature for browser compatibility */ anchor(name: string): string; @@ -210,7 +209,7 @@ interface String { fontsize(size: string): string; /** - * Returns a `` HTML element + * Returns an `` HTML element * @deprecated A legacy feature for browser compatibility */ italics(): string; diff --git a/lib/lib.es5.d.ts b/lib/lib.es5.d.ts index 455064b..a8ee16e 100644 --- a/lib/lib.es5.d.ts +++ b/lib/lib.es5.d.ts @@ -8,6 +8,68 @@ type UnionToIntersection = ( ? F : unknown; +/* +type Item = number extends A["length"] + ? T | undefined + : A["length"] extends 0 + ? undefined + : T; +*/ + +type ReverseArray = T extends [ + infer F, + ...infer R +] + ? ReverseArray + : U; + +type Reverse = number extends A["length"] + ? T[] + : ReverseArray; + +type RangeArray = A["length"] extends N + ? A + : RangeArray; + +type Indices = number extends A["length"] + ? number + : RangeArray extends (infer A)[] + ? A + : never; + +type FilterArray< + T extends readonly unknown[], + S, + U extends unknown[] +> = T extends readonly [infer F, ...infer R] + ? FilterArray extends S ? [...U, First] : U> + : U; + +type Filter = number extends T["length"] + ? S[] + : FilterArray<{ [K in keyof T]: [T[K]] }, S, []>; + +type Cast = T extends U ? T : U; + +type ValueIndexPairArray< + T extends readonly unknown[], + A extends unknown[] = [] +> = A["length"] extends T["length"] + ? A + : ValueIndexPairArray< + T, + [...A, [value: T[A["length"]], index: A["length"], array: T]] + >; + +type ValueIndexPair = Cast< + number extends A["length"] + ? unknown + : ValueIndexPairArray extends (infer R)[] + ? R + : unknown, + [value: T, index: number, array: A] +>; + /** * Evaluates JavaScript code and executes it. * @param x A String value that contains valid JavaScript code. @@ -609,13 +671,13 @@ interface ReadonlyArray { * @param searchElement The value to locate in the array. * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0. */ - indexOf(searchElement: T, fromIndex?: number): number; + indexOf(searchElement: T, fromIndex?: number): -1 | Indices; /** * Returns the index of the last occurrence of a specified value in an array. * @param searchElement The value to locate in the array. * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at the last index in the array. */ - lastIndexOf(searchElement: T, fromIndex?: number): number; + lastIndexOf(searchElement: T, fromIndex?: number): -1 | Indices; /** * Determines whether all the members of an array satisfy the specified test. * @param predicate A function that accepts up to three arguments. The every method calls @@ -628,11 +690,11 @@ interface ReadonlyArray { predicate: ( this: This, value: T, - index: number, - array: readonly T[] + index: Indices, + array: this ) => value is S, thisArg?: This - ): this is readonly S[]; + ): this is { [K in keyof this]: S }; /** * Determines whether all the members of an array satisfy the specified test. * @param predicate A function that accepts up to three arguments. The every method calls @@ -642,12 +704,7 @@ interface ReadonlyArray { * If thisArg is omitted, undefined is used as the this value. */ every( - predicate: ( - this: This, - value: T, - index: number, - array: readonly T[] - ) => boolean, + predicate: (this: This, ...args: ValueIndexPair) => boolean, thisArg?: This ): boolean; /** @@ -659,12 +716,7 @@ interface ReadonlyArray { * If thisArg is omitted, undefined is used as the this value. */ some( - predicate: ( - this: This, - value: T, - index: number, - array: readonly T[] - ) => boolean, + predicate: (this: This, ...args: ValueIndexPair) => boolean, thisArg?: This ): boolean; /** @@ -673,12 +725,7 @@ interface ReadonlyArray { * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. */ forEach( - callbackfn: ( - this: This, - value: T, - index: number, - array: readonly T[] - ) => void, + callbackfn: (this: This, ...args: ValueIndexPair) => void, thisArg?: This ): void; /** @@ -687,9 +734,9 @@ interface ReadonlyArray { * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. */ map( - callbackfn: (this: This, value: T, index: number, array: readonly T[]) => U, + callbackfn: (this: This, ...args: ValueIndexPair) => U, thisArg?: This - ): U[]; + ): { -readonly [K in keyof this]: U }; /** * Returns the elements of an array that meet the condition specified in a callback function. * @param predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array. @@ -699,23 +746,18 @@ interface ReadonlyArray { predicate: ( this: This, value: T, - index: number, - array: readonly T[] + index: Indices, + array: this ) => value is S, thisArg?: This - ): S[]; + ): Filter; /** * Returns the elements of an array that meet the condition specified in a callback function. * @param predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array. * @param thisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value. */ filter( - predicate: ( - this: This, - value: T, - index: number, - array: readonly T[] - ) => boolean, + predicate: (this: This, ...args: ValueIndexPair) => boolean, thisArg?: This ): T[]; /** @@ -723,35 +765,16 @@ interface ReadonlyArray { * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array. * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. */ - reduce( - callbackfn: ( - previousValue: T, - currentValue: T, - currentIndex: number, - array: readonly T[] - ) => T - ): T; - reduce( - callbackfn: ( - previousValue: T, - currentValue: T, - currentIndex: number, - array: readonly T[] - ) => T, - initialValue: T - ): T; + reduce( + callbackfn: (previousValue: T | U, ...args: ValueIndexPair) => U + ): U; /** * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array. * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. */ - reduce( - callbackfn: ( - previousValue: U, - currentValue: T, - currentIndex: number, - array: readonly T[] - ) => U, + reduce( + callbackfn: (previousValue: U, ...args: ValueIndexPair) => U, initialValue: U ): U; /** @@ -759,35 +782,16 @@ interface ReadonlyArray { * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array. * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. */ - reduceRight( - callbackfn: ( - previousValue: T, - currentValue: T, - currentIndex: number, - array: readonly T[] - ) => T - ): T; - reduceRight( - callbackfn: ( - previousValue: T, - currentValue: T, - currentIndex: number, - array: readonly T[] - ) => T, - initialValue: T - ): T; + reduceRight( + callbackfn: (previousValue: T | U, ...args: ValueIndexPair) => U + ): U; /** * Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array. * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. */ - reduceRight( - callbackfn: ( - previousValue: U, - currentValue: T, - currentIndex: number, - array: readonly T[] - ) => U, + reduceRight( + callbackfn: (previousValue: U, ...args: ValueIndexPair) => U, initialValue: U ): U; @@ -838,7 +842,7 @@ interface Array { * Reverses the elements in an array in place. * This method mutates the array and returns a reference to the same array. */ - reverse(): T[]; + reverse(): Reverse; /** * Removes the first element from an array and returns it. * If the array is empty, undefined is returned and the array is not modified. @@ -864,14 +868,14 @@ interface Array { * [11,2,22,1].sort((a, b) => a - b) * ``` */ - sort(compareFn?: (a: T, b: T) => number): this; + sort(compareFn?: (a: T, b: T) => number): T[]; /** * Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements. * @param start The zero-based location in the array from which to start removing elements. * @param deleteCount The number of elements to remove. * @returns An array containing the elements that were deleted. */ - splice(start: number, deleteCount?: number): this; + splice(start: number, deleteCount?: number): T[]; /** * Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements. * @param start The zero-based location in the array from which to start removing elements. @@ -879,7 +883,7 @@ interface Array { * @param items Elements to insert into the array in place of the deleted elements. * @returns An array containing the elements that were deleted. */ - splice(start: number, deleteCount: number, ...items: T[]): this; + splice(start: number, deleteCount: number, ...items: T[]): T[]; /** * Inserts new elements at the start of an array, and returns the new length of the array. * @param items Elements to insert at the start of the array. @@ -888,15 +892,13 @@ interface Array { /** * Returns the index of the first occurrence of a value in an array, or -1 if it is not present. * @param searchElement The value to locate in the array. - * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0. */ - indexOf(searchElement: T, fromIndex?: number): number; + indexOf(searchElement: T, fromIndex?: number): -1 | Indices; /** * Returns the index of the last occurrence of a specified value in an array, or -1 if it is not present. * @param searchElement The value to locate in the array. - * @param fromIndex The array index at which to begin searching backward. If fromIndex is omitted, the search starts at the last index in the array. */ - lastIndexOf(searchElement: T, fromIndex?: number): number; + lastIndexOf(searchElement: T, fromIndex?: number): -1 | Indices; /** * Determines whether all the members of an array satisfy the specified test. * @param predicate A function that accepts up to three arguments. The every method calls @@ -906,9 +908,14 @@ interface Array { * If thisArg is omitted, undefined is used as the this value. */ every( - predicate: (this: This, value: T, index: number, array: T[]) => value is S, + predicate: ( + this: This, + value: T, + index: Indices, + array: this + ) => value is S, thisArg?: This - ): this is S[]; + ): this is { [K in keyof this]: S }; /** * Determines whether all the members of an array satisfy the specified test. * @param predicate A function that accepts up to three arguments. The every method calls @@ -918,7 +925,7 @@ interface Array { * If thisArg is omitted, undefined is used as the this value. */ every( - predicate: (this: This, value: T, index: number, array: T[]) => boolean, + predicate: (this: This, ...args: ValueIndexPair) => boolean, thisArg?: This ): boolean; /** @@ -930,7 +937,7 @@ interface Array { * If thisArg is omitted, undefined is used as the this value. */ some( - predicate: (this: This, value: T, index: number, array: T[]) => boolean, + predicate: (this: This, ...args: ValueIndexPair) => boolean, thisArg?: This ): boolean; /** @@ -939,7 +946,7 @@ interface Array { * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. */ forEach( - callbackfn: (this: This, value: T, index: number, array: T[]) => void, + callbackfn: (this: This, ...args: ValueIndexPair) => void, thisArg?: This ): void; /** @@ -948,97 +955,62 @@ interface Array { * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. */ map( - callbackfn: (this: This, value: T, index: number, array: T[]) => U, + callbackfn: (this: This, ...args: ValueIndexPair) => U, thisArg?: This - ): U[]; + ): { [K in keyof this]: U }; /** * Returns the elements of an array that meet the condition specified in a callback function. * @param predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array. * @param thisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value. */ filter( - predicate: (this: This, value: T, index: number, array: T[]) => value is S, + predicate: ( + this: This, + value: T, + index: Indices, + array: this + ) => value is S, thisArg?: This - ): S[]; + ): Filter; /** * Returns the elements of an array that meet the condition specified in a callback function. * @param predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array. * @param thisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value. */ filter( - predicate: (this: This, value: T, index: number, array: T[]) => boolean, + predicate: (this: This, ...args: ValueIndexPair) => boolean, thisArg?: This ): T[]; /** * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array. - * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. */ - reduce( - callbackfn: ( - previousValue: T, - currentValue: T, - currentIndex: number, - array: T[] - ) => T - ): T; - reduce( - callbackfn: ( - previousValue: T, - currentValue: T, - currentIndex: number, - array: T[] - ) => T, - initialValue: T - ): T; + reduce( + callbackfn: (previousValue: T | U, ...args: ValueIndexPair) => U + ): U; /** * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array. * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. */ - reduce( - callbackfn: ( - previousValue: U, - currentValue: T, - currentIndex: number, - array: T[] - ) => U, + reduce( + callbackfn: (previousValue: U, ...args: ValueIndexPair) => U, initialValue: U ): U; /** * Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array. - * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. */ - reduceRight( - callbackfn: ( - previousValue: T, - currentValue: T, - currentIndex: number, - array: T[] - ) => T - ): T; - reduceRight( - callbackfn: ( - previousValue: T, - currentValue: T, - currentIndex: number, - array: T[] - ) => T, - initialValue: T - ): T; + reduceRight( + callbackfn: (previousValue: T | U, ...args: ValueIndexPair) => U + ): U; /** * Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array. * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. */ - reduceRight( - callbackfn: ( - previousValue: U, - currentValue: T, - currentIndex: number, - array: T[] - ) => U, + reduceRight( + callbackfn: (previousValue: U, ...args: ValueIndexPair) => U, initialValue: U ): U; diff --git a/lib/lib.util.d.ts b/lib/lib.util.d.ts index b78d156..7d1000e 100644 --- a/lib/lib.util.d.ts +++ b/lib/lib.util.d.ts @@ -7,3 +7,65 @@ type UnionToIntersection = ( ) extends (arg: infer F) => void ? F : unknown; + +/* +type Item = number extends A["length"] + ? T | undefined + : A["length"] extends 0 + ? undefined + : T; +*/ + +type ReverseArray = T extends [ + infer F, + ...infer R +] + ? ReverseArray + : U; + +type Reverse = number extends A["length"] + ? T[] + : ReverseArray; + +type RangeArray = A["length"] extends N + ? A + : RangeArray; + +type Indices = number extends A["length"] + ? number + : RangeArray extends (infer A)[] + ? A + : never; + +type FilterArray< + T extends readonly unknown[], + S, + U extends unknown[] +> = T extends readonly [infer F, ...infer R] + ? FilterArray extends S ? [...U, First] : U> + : U; + +type Filter = number extends T["length"] + ? S[] + : FilterArray<{ [K in keyof T]: [T[K]] }, S, []>; + +type Cast = T extends U ? T : U; + +type ValueIndexPairArray< + T extends readonly unknown[], + A extends unknown[] = [] +> = A["length"] extends T["length"] + ? A + : ValueIndexPairArray< + T, + [...A, [value: T[A["length"]], index: A["length"], array: T]] + >; + +type ValueIndexPair = Cast< + number extends A["length"] + ? unknown + : ValueIndexPairArray extends (infer R)[] + ? R + : unknown, + [value: T, index: number, array: A] +>; diff --git a/tests/src/es5.ts b/tests/src/es5.ts index 3bd7890..b2d7e85 100644 --- a/tests/src/es5.ts +++ b/tests/src/es5.ts @@ -173,34 +173,288 @@ expectType<{ foo: number; bar: string; baz: boolean }>( expectType(JSON.stringify(o)); } +let reduce!: "reduce" | "reduceRight"; + // ReadonlyArray { // https://github.com/uhyo/better-typescript-lib/issues/7 const a1: readonly number[] = [1, 2, 3]; - expectType(a1.filter((a1) => a1 > 2)); - expectType<1[]>(a1.filter((x): x is 1 => x === 1)); - if (a1.every((x): x is 2 => x === 2)) { + expectError(a1.reverse()); + expectType(a1.indexOf(1)); + expectType(a1.lastIndexOf(1)); + + expectType( + a1.filter((x, index) => { + expectType(x); + expectType(index); + return x > 2; + }) + ); + expectType<1[]>( + a1.filter((x, index): x is 1 => { + expectType(x); + expectType(index); + return x === 1; + }) + ); + if ( + a1.every((x, index): x is 2 => { + expectType(x); + expectType(index); + return x === 2; + }) + ) { expectType(a1); } + a1.some((...[x, index]) => { + expectType(x); + expectType(index); + return true; + }); + a1.forEach((...[x, index]) => { + expectType(x); + expectType(index); + }); + expectType<"bar"[]>( + a1.map((...[x, index]) => { + expectType(x); + expectType(index); + return "bar" as const; + }) + ); + + expectType<"bar">( + a1[reduce]<"bar">((p, x, index) => { + expectType(p); + expectType(x); + expectType(index); + return "bar"; + }) + ); + expectType<"bar">( + a1[reduce]<"bar">((p, x, index) => { + expectType<"bar">(p); + expectType(x); + expectType(index); + return "bar"; + }, "bar") + ); + expectError(a1.filter((x) => x)); expectError(a1.every((x) => x)); expectError(a1.some((x) => x)); + + // Tuple + const t1 = ["foo", 42, true] as const; + expectError(t1.reverse()); + expectType<-1 | 0 | 1 | 2>(t1.indexOf(true)); + expectType<-1 | 0 | 1 | 2>(t1.lastIndexOf(true)); + + // https://github.com/microsoft/TypeScript/issues/48663 + expectType<("foo" | 42 | true)[]>( + t1.filter((...[x, index]) => { + expectType<"foo" | 42 | true>(x); + expectType<0 | 1 | 2>(index); + return x === 42; + }) + ); + expectType<[42]>( + t1.filter((x, index): x is 42 => { + expectType<"foo" | 42 | true>(x); + expectType<0 | 1 | 2>(index); + return x === 42; + }) + ); + const t2: readonly [number, number, number] = [42, 42, 42]; + if ( + t2.every((x, index): x is 42 => { + expectType(x); + expectType<0 | 1 | 2>(index); + return x === 42; + }) + ) { + expectType(t2); + } + + t1.some((...[x, index]) => { + expectType<"foo" | 42 | true>(x); + expectType<0 | 1 | 2>(index); + return true; + }); + t1.forEach((...[x, index]) => { + expectType<"foo" | 42 | true>(x); + expectType<0 | 1 | 2>(index); + }); + expectType<["bar", "bar", "bar"]>( + t1.map((...[x, index]) => { + expectType<"foo" | 42 | true>(x); + expectType<0 | 1 | 2>(index); + return "bar" as const; + }) + ); + + expectType<"bar">( + t1[reduce]<"bar">((p, ...[x, index]) => { + expectType<"foo" | 42 | true | "bar">(p); + expectType<"foo" | 42 | true>(x); + expectType<0 | 1 | 2>(index); + return "bar"; + }) + ); + expectType<"bar">( + t1[reduce]<"bar">((p, ...[x, index]) => { + expectType<"bar">(p); + expectType<"foo" | 42 | true>(x); + expectType<0 | 1 | 2>(index); + return "bar"; + }, "bar") + ); + + expectError(t1.filter((x) => x)); + expectError(t1.every((x) => x)); + expectError(t1.some((x) => x)); } // Array { // https://github.com/uhyo/better-typescript-lib/issues/7 const a1: number[] = [1, 2, 3]; - expectType(a1.filter((a1) => a1 > 2)); - expectType<1[]>(a1.filter((x): x is 1 => x === 1)); - if (a1.every((x): x is 2 => x === 2)) { + expectType(a1.reverse()); + expectType(a1.indexOf(1)); + expectType(a1.lastIndexOf(1)); + + expectType( + a1.filter((x, index) => { + expectType(x); + expectType(index); + return x > 2; + }) + ); + expectType<1[]>( + a1.filter((x, index): x is 1 => { + expectType(x); + expectType(index); + return x === 1; + }) + ); + if ( + a1.every((x, index): x is 2 => { + expectType(x); + expectType(index); + return x === 2; + }) + ) { expectType<2[]>(a1); } + a1.some((...[x, index]) => { + expectType(x); + expectType(index); + return true; + }); + a1.forEach((...[x, index]) => { + expectType(x); + expectType(index); + }); + expectType<"bar"[]>( + a1.map((...[x, index]) => { + expectType(x); + expectType(index); + return "bar" as const; + }) + ); + + expectType<"bar">( + a1[reduce]<"bar">((p, x, index) => { + expectType(p); + expectType(x); + expectType(index); + return "bar"; + }) + ); + expectType<"bar">( + a1[reduce]<"bar">((p, x, index) => { + expectType<"bar">(p); + expectType(x); + expectType(index); + return "bar"; + }, "bar") + ); + expectError(a1.filter((x) => x)); expectError(a1.every((x) => x)); expectError(a1.some((x) => x)); + + // Tuple + const t1: ["foo", 42, true] = ["foo", 42, true]; + expectType<[true, 42, "foo"]>(t1.reverse()); + expectType<-1 | 0 | 1 | 2>(t1.indexOf(true)); + expectType<-1 | 0 | 1 | 2>(t1.lastIndexOf(true)); + + // https://github.com/microsoft/TypeScript/issues/48663 + expectType<("foo" | 42 | true)[]>( + t1.filter((...[x, index]) => { + expectType<"foo" | 42 | true>(x); + expectType<0 | 1 | 2>(index); + return x === 42; + }) + ); + expectType<[42]>( + t1.filter((x, index): x is 42 => { + expectType<"foo" | 42 | true>(x); + expectType<0 | 1 | 2>(index); + return x === 42; + }) + ); + const t2: [number, number, number] = [42, 42, 42]; + if ( + t2.every((x, index): x is 42 => { + expectType(x); + expectType<0 | 1 | 2>(index); + return x === 42; + }) + ) { + expectType<[42, 42, 42]>(t2); + } + + t1.some((...[x, index]) => { + expectType<"foo" | 42 | true>(x); + expectType<0 | 1 | 2>(index); + return true; + }); + t1.forEach((...[x, index]) => { + expectType<"foo" | 42 | true>(x); + expectType<0 | 1 | 2>(index); + }); + expectType<["bar", "bar", "bar"]>( + t1.map((...[x, index]) => { + expectType<"foo" | 42 | true>(x); + expectType<0 | 1 | 2>(index); + return "bar" as const; + }) + ); + + expectType<"bar">( + t1[reduce]<"bar">((p, ...[x, index]) => { + expectType<"foo" | 42 | true | "bar">(p); + expectType<"foo" | 42 | true>(x); + expectType<0 | 1 | 2>(index); + return "bar"; + }) + ); + expectType<"bar">( + t1[reduce]<"bar">((p, ...[x, index]) => { + expectType<"bar">(p); + expectType<"foo" | 42 | true>(x); + expectType<0 | 1 | 2>(index); + return "bar"; + }, "bar") + ); + + expectError(t1.filter((x) => x)); + expectError(t1.every((x) => x)); + expectError(t1.some((x) => x)); } // ArrayConstructor From a058c2bf2f2c2992602a1e7bfa0279000d384334 Mon Sep 17 00:00:00 2001 From: graphemecluster Date: Fri, 3 Jun 2022 07:40:17 +0800 Subject: [PATCH 2/2] Correct utilities --- docs/diff/es5.d.ts.md | 82 +++++++++++++++++++----------------------- generated/lib.es5.d.ts | 38 ++++++++------------ lib/lib.es5.d.ts | 38 ++++++++------------ lib/lib.util.d.ts | 36 ++++++++----------- 4 files changed, 81 insertions(+), 113 deletions(-) diff --git a/docs/diff/es5.d.ts.md b/docs/diff/es5.d.ts.md index 334cf50..1e930ba 100644 --- a/docs/diff/es5.d.ts.md +++ b/docs/diff/es5.d.ts.md @@ -5,7 +5,7 @@ Index: es5.d.ts =================================================================== --- es5.d.ts +++ es5.d.ts -@@ -1,4 +1,85 @@ +@@ -1,4 +1,77 @@ +/// + +type First = T extends [any] ? T[0] : unknown; @@ -16,25 +16,17 @@ Index: es5.d.ts + ? F + : unknown; + -+/* -+type Item = number extends A["length"] -+ ? T | undefined -+ : A["length"] extends 0 -+ ? undefined -+ : T; -+*/ -+ -+type ReverseArray = T extends [ ++type Reverse = T extends [ + infer F, + ...infer R +] -+ ? ReverseArray ++ ? Reverse ++ : T extends [...infer R, infer L] ++ ? [L, ...Reverse] ++ : T extends unknown[] ++ ? [...T, ...U] + : U; + -+type Reverse = number extends A["length"] -+ ? T[] -+ : ReverseArray; -+ +type RangeArray = A["length"] extends N + ? A + : RangeArray; @@ -45,17 +37,17 @@ Index: es5.d.ts + ? A + : never; + -+type FilterArray< ++type FilterMatch = T extends Readonly ? T : U extends T ? U | [] : []; ++ ++type Filter< + T extends readonly unknown[], + S, -+ U extends unknown[] ++ U extends unknown[] = [] +> = T extends readonly [infer F, ...infer R] -+ ? FilterArray extends S ? [...U, First] : U> -+ : U; -+ -+type Filter = number extends T["length"] -+ ? S[] -+ : FilterArray<{ [K in keyof T]: [T[K]] }, S, []>; ++ ? Filter]> ++ : T extends readonly [...infer R, infer L] ++ ? [...Filter, ...FilterMatch<[L], [S]>] ++ : [...U, ...FilterMatch]; + +type Cast = T extends U ? T : U; + @@ -91,7 +83,7 @@ Index: es5.d.ts /// ECMAScript APIs ///////////////////////////// -@@ -8,9 +89,9 @@ +@@ -8,9 +81,9 @@ /** * Evaluates JavaScript code and executes it. * @param x A String value that contains valid JavaScript code. @@ -102,7 +94,7 @@ Index: es5.d.ts /** * Converts a string to an integer. * @param string A string to convert into a number. -@@ -117,9 +198,19 @@ +@@ -117,9 +190,19 @@ /** * Determines whether an object has a property with the specified name. * @param v A property name. @@ -123,7 +115,7 @@ Index: es5.d.ts /** * Determines whether an object exists in another object's prototype chain. * @param v Another object whose prototype chain is to be checked. -@@ -132,21 +223,26 @@ +@@ -132,21 +215,26 @@ */ propertyIsEnumerable(v: PropertyKey): boolean; } @@ -153,7 +145,7 @@ Index: es5.d.ts /** * Gets the own property descriptor of the specified object. * An own property descriptor is one that is defined directly on the object and is not inherited from the object's prototype. -@@ -162,47 +258,101 @@ +@@ -162,47 +250,101 @@ * Returns the names of the own properties of an object. The own properties of an object are those that are defined directly * on that object, and are not inherited from the object's prototype. The properties of an object include both fields (objects) and functions. * @param o Object that contains the own properties. @@ -270,7 +262,7 @@ Index: es5.d.ts /** * Prevents the modification of attributes of existing properties, and prevents the addition of new properties. * @param o Object on which to lock the attributes. -@@ -210,17 +360,17 @@ +@@ -210,17 +352,17 @@ seal(o: T): T; /** @@ -292,7 +284,7 @@ Index: es5.d.ts /** * Prevents the modification of existing property attributes and values, and prevents the addition of new properties. * @param o Object on which to lock the attributes. -@@ -258,13 +408,8 @@ +@@ -258,13 +400,8 @@ keys(o: object): string[]; } @@ -306,7 +298,7 @@ Index: es5.d.ts */ interface Function { /** -@@ -331,11 +476,16 @@ +@@ -331,11 +468,16 @@ interface CallableFunction extends Function { /** * Calls the function with the specified object as the this value and the elements of specified array as the arguments. @@ -324,7 +316,7 @@ Index: es5.d.ts this: (this: T, ...args: A) => R, thisArg: T, args: A -@@ -357,49 +507,27 @@ +@@ -357,49 +499,27 @@ * The this object of the bound function is associated with the specified object, and has the specified initial parameters. * @param thisArg The object to be used as the this object. * @param args Arguments to bind to the parameters of the function. @@ -384,7 +376,7 @@ Index: es5.d.ts this: new (...args: A) => T, thisArg: T, args: A -@@ -421,44 +549,17 @@ +@@ -421,44 +541,17 @@ * The this object of the bound function is associated with the specified object, and has the specified initial parameters. * @param thisArg The object to be used as the this object. * @param args Arguments to bind to the parameters of the function. @@ -434,7 +426,7 @@ Index: es5.d.ts callee: Function; } -@@ -511,21 +612,26 @@ +@@ -511,21 +604,26 @@ match(regexp: string | RegExp): RegExpMatchArray | null; /** @@ -464,7 +456,7 @@ Index: es5.d.ts /** * Finds the first substring match in a regular expression search. -@@ -586,8 +692,24 @@ +@@ -586,8 +684,24 @@ /** Returns the primitive value of the specified object. */ valueOf(): string; @@ -489,7 +481,7 @@ Index: es5.d.ts interface StringConstructor { new (value?: any): String; -@@ -595,13 +717,8 @@ +@@ -595,13 +709,8 @@ readonly prototype: String; fromCharCode(...codes: number[]): string; } @@ -503,7 +495,7 @@ Index: es5.d.ts /** Returns the primitive value of the specified object. */ valueOf(): boolean; } -@@ -1187,43 +1304,81 @@ +@@ -1187,43 +1296,81 @@ * If a member contains nested objects, the nested objects are transformed before the parent object is. */ parse( @@ -598,7 +590,7 @@ Index: es5.d.ts /** * Gets the length of the array. This is a number one higher than the highest element defined in an array. */ -@@ -1261,38 +1416,43 @@ +@@ -1261,38 +1408,43 @@ * Returns the index of the first occurrence of a value in an array. * @param searchElement The value to locate in the array. * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0. @@ -651,7 +643,7 @@ Index: es5.d.ts /** * Determines whether the specified callback function returns true for any element of an array. * @param predicate A function that accepts up to three arguments. The some method calls -@@ -1300,118 +1460,85 @@ +@@ -1300,118 +1452,85 @@ * which is coercible to the Boolean value true, or until the end of the array. * @param thisArg An object to which the this keyword can refer in the predicate function. * If thisArg is omitted, undefined is used as the this value. @@ -802,18 +794,18 @@ Index: es5.d.ts ): U; readonly [n: number]: T; -@@ -1467,9 +1594,9 @@ +@@ -1467,9 +1586,9 @@ /** * Reverses the elements in an array in place. * This method mutates the array and returns a reference to the same array. */ - reverse(): T[]; -+ reverse(): Reverse; ++ reverse(): Reverse; /** * Removes the first element from an array and returns it. * If the array is empty, undefined is returned and the array is not modified. */ -@@ -1493,9 +1620,9 @@ +@@ -1493,9 +1612,9 @@ * ```ts * [11,2,22,1].sort((a, b) => a - b) * ``` @@ -824,7 +816,7 @@ Index: es5.d.ts * Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements. * @param start The zero-based location in the array from which to start removing elements. * @param deleteCount The number of elements to remove. -@@ -1517,40 +1644,43 @@ +@@ -1517,40 +1636,43 @@ unshift(...items: T[]): number; /** * Returns the index of the first occurrence of a value in an array, or -1 if it is not present. @@ -879,7 +871,7 @@ Index: es5.d.ts /** * Determines whether the specified callback function returns true for any element of an array. * @param predicate A function that accepts up to three arguments. The some method calls -@@ -1558,137 +1688,100 @@ +@@ -1558,137 +1680,100 @@ * which is coercible to the Boolean value true, or until the end of the array. * @param thisArg An object to which the this keyword can refer in the predicate function. * If thisArg is omitted, undefined is used as the this value. @@ -1053,7 +1045,7 @@ Index: es5.d.ts enumerable?: boolean; configurable?: boolean; writable?: boolean; -@@ -1716,9 +1809,15 @@ +@@ -1716,9 +1801,15 @@ ) => void; declare type PromiseConstructorLike = new ( @@ -1070,7 +1062,7 @@ Index: es5.d.ts ) => void ) => PromiseLike; -@@ -5435,22 +5534,8 @@ +@@ -5435,22 +5526,8 @@ ): string[]; }; } diff --git a/generated/lib.es5.d.ts b/generated/lib.es5.d.ts index c2f0e62..fcdc7bc 100644 --- a/generated/lib.es5.d.ts +++ b/generated/lib.es5.d.ts @@ -8,25 +8,17 @@ type UnionToIntersection = ( ? F : unknown; -/* -type Item = number extends A["length"] - ? T | undefined - : A["length"] extends 0 - ? undefined - : T; -*/ - -type ReverseArray = T extends [ +type Reverse = T extends [ infer F, ...infer R ] - ? ReverseArray + ? Reverse + : T extends [...infer R, infer L] + ? [L, ...Reverse] + : T extends unknown[] + ? [...T, ...U] : U; -type Reverse = number extends A["length"] - ? T[] - : ReverseArray; - type RangeArray = A["length"] extends N ? A : RangeArray; @@ -37,17 +29,17 @@ type Indices = number extends A["length"] ? A : never; -type FilterArray< +type FilterMatch = T extends Readonly ? T : U extends T ? U | [] : []; + +type Filter< T extends readonly unknown[], S, - U extends unknown[] + U extends unknown[] = [] > = T extends readonly [infer F, ...infer R] - ? FilterArray extends S ? [...U, First] : U> - : U; - -type Filter = number extends T["length"] - ? S[] - : FilterArray<{ [K in keyof T]: [T[K]] }, S, []>; + ? Filter]> + : T extends readonly [...infer R, infer L] + ? [...Filter, ...FilterMatch<[L], [S]>] + : [...U, ...FilterMatch]; type Cast = T extends U ? T : U; @@ -2120,7 +2112,7 @@ interface Array { * Reverses the elements in an array in place. * This method mutates the array and returns a reference to the same array. */ - reverse(): Reverse; + reverse(): Reverse; /** * Removes the first element from an array and returns it. * If the array is empty, undefined is returned and the array is not modified. diff --git a/lib/lib.es5.d.ts b/lib/lib.es5.d.ts index a8ee16e..bb2f8a9 100644 --- a/lib/lib.es5.d.ts +++ b/lib/lib.es5.d.ts @@ -8,25 +8,17 @@ type UnionToIntersection = ( ? F : unknown; -/* -type Item = number extends A["length"] - ? T | undefined - : A["length"] extends 0 - ? undefined - : T; -*/ - -type ReverseArray = T extends [ +type Reverse = T extends [ infer F, ...infer R ] - ? ReverseArray + ? Reverse + : T extends [...infer R, infer L] + ? [L, ...Reverse] + : T extends unknown[] + ? [...T, ...U] : U; -type Reverse = number extends A["length"] - ? T[] - : ReverseArray; - type RangeArray = A["length"] extends N ? A : RangeArray; @@ -37,17 +29,17 @@ type Indices = number extends A["length"] ? A : never; -type FilterArray< +type FilterMatch = T extends Readonly ? T : U extends T ? U | [] : []; + +type Filter< T extends readonly unknown[], S, - U extends unknown[] + U extends unknown[] = [] > = T extends readonly [infer F, ...infer R] - ? FilterArray extends S ? [...U, First] : U> - : U; - -type Filter = number extends T["length"] - ? S[] - : FilterArray<{ [K in keyof T]: [T[K]] }, S, []>; + ? Filter]> + : T extends readonly [...infer R, infer L] + ? [...Filter, ...FilterMatch<[L], [S]>] + : [...U, ...FilterMatch]; type Cast = T extends U ? T : U; @@ -842,7 +834,7 @@ interface Array { * Reverses the elements in an array in place. * This method mutates the array and returns a reference to the same array. */ - reverse(): Reverse; + reverse(): Reverse; /** * Removes the first element from an array and returns it. * If the array is empty, undefined is returned and the array is not modified. diff --git a/lib/lib.util.d.ts b/lib/lib.util.d.ts index 7d1000e..8b477c2 100644 --- a/lib/lib.util.d.ts +++ b/lib/lib.util.d.ts @@ -8,25 +8,17 @@ type UnionToIntersection = ( ? F : unknown; -/* -type Item = number extends A["length"] - ? T | undefined - : A["length"] extends 0 - ? undefined - : T; -*/ - -type ReverseArray = T extends [ +type Reverse = T extends [ infer F, ...infer R ] - ? ReverseArray + ? Reverse + : T extends [...infer R, infer L] + ? [L, ...Reverse] + : T extends unknown[] + ? [...T, ...U] : U; -type Reverse = number extends A["length"] - ? T[] - : ReverseArray; - type RangeArray = A["length"] extends N ? A : RangeArray; @@ -37,17 +29,17 @@ type Indices = number extends A["length"] ? A : never; -type FilterArray< +type FilterMatch = T extends Readonly ? T : U extends T ? U | [] : []; + +type Filter< T extends readonly unknown[], S, - U extends unknown[] + U extends unknown[] = [] > = T extends readonly [infer F, ...infer R] - ? FilterArray extends S ? [...U, First] : U> - : U; - -type Filter = number extends T["length"] - ? S[] - : FilterArray<{ [K in keyof T]: [T[K]] }, S, []>; + ? Filter]> + : T extends readonly [...infer R, infer L] + ? [...Filter, ...FilterMatch<[L], [S]>] + : [...U, ...FilterMatch]; type Cast = T extends U ? T : U;