mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-22 22:55:36 -05:00
Variadic tuple types (#39094)
* Initial implementation of variadic tuple types * Accept new baselines * Handle variadic elements in tuple type inference * Special case inference between tuples with matching structure * Restore check that rest element is last element * Handle variadic tuples in relationship checking * Accept new baselines * Infer readonly constraints when inferring from readonly tuples * Fix lint issues * T assignable to readonly [...T] and [...T] assignable to T * Consistent tuple normalization * Create variadic tuple types from array literal expressions * Accept new baselines * Array literals have tuple types when contextual type is readonly * Accept new baselines * Optional elements before required elements become required elements * Update logic for rest parameters and spread arguments * Revert special case of contextual readonly array type * Accept new baselines * Fix lint issue * Switch entirely to createTupleType based on element flags * Don't infer readonly tuple types when inferring to variadic elements * Handle mapped types applied to generic tuple types * Handle constraint of indexed access type with generic tuple type * Accept new baselines * Address CR feedback * Simplify indexed access types involving generic tuple types * Propagate checkMode into getSpreadArgumentType * Guard against missing globalArrayType * Inference to [...T, ...U] based on implied arity of T * Accept new baselines * Add tests * Emit .d.ts from tests * Address CR feedback
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -1201,6 +1201,14 @@ namespace ts {
|
||||
return values;
|
||||
}
|
||||
|
||||
export function arrayOf<T>(count: number, f: (index: number) => T): T[] {
|
||||
const result = new Array(count);
|
||||
for (let i = 0; i < count; i++) {
|
||||
result[i] = f(i);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Shims `Array.from`. */
|
||||
export function arrayFrom<T, U>(iterator: Iterator<T> | IterableIterator<T>, map: (t: T) => U): U[];
|
||||
export function arrayFrom<T>(iterator: Iterator<T> | IterableIterator<T>): T[];
|
||||
|
||||
@@ -2414,6 +2414,26 @@
|
||||
"category": "Error",
|
||||
"code": 2617
|
||||
},
|
||||
"Source has {0} element(s) but target requires {1}.": {
|
||||
"category": "Error",
|
||||
"code": 2618
|
||||
},
|
||||
"Source has {0} element(s) but target allows only {1}.": {
|
||||
"category": "Error",
|
||||
"code": 2619
|
||||
},
|
||||
"Target requires {0} element(s) but source may have fewer.": {
|
||||
"category": "Error",
|
||||
"code": 2620
|
||||
},
|
||||
"Target allows only {0} element(s) but source may have more.": {
|
||||
"category": "Error",
|
||||
"code": 2621
|
||||
},
|
||||
"Element at index {0} is variadic in one type but not in the other.": {
|
||||
"category": "Error",
|
||||
"code": 2622
|
||||
},
|
||||
|
||||
"Cannot augment module '{0}' with value exports because it resolves to a non-module entity.": {
|
||||
"category": "Error",
|
||||
|
||||
@@ -5100,9 +5100,20 @@ namespace ts {
|
||||
variances?: VarianceFlags[]; // Variance of each type parameter
|
||||
}
|
||||
|
||||
export const enum ElementFlags {
|
||||
Required = 1 << 0, // T
|
||||
Optional = 1 << 1, // T?
|
||||
Rest = 1 << 2, // ...T[]
|
||||
Variadic = 1 << 3, // ...T
|
||||
Variable = Rest | Variadic,
|
||||
}
|
||||
|
||||
export interface TupleType extends GenericType {
|
||||
elementFlags: readonly ElementFlags[];
|
||||
minLength: number;
|
||||
fixedLength: number;
|
||||
hasRestElement: boolean;
|
||||
combinedFlags: ElementFlags;
|
||||
readonly: boolean;
|
||||
labeledElementDeclarations?: readonly (NamedTupleMember | ParameterDeclaration)[];
|
||||
}
|
||||
@@ -5422,6 +5433,7 @@ namespace ts {
|
||||
priority?: InferencePriority; // Priority of current inference set
|
||||
topLevel: boolean; // True if all inferences are to top level occurrences
|
||||
isFixed: boolean; // True if inferences are fixed
|
||||
impliedArity?: number;
|
||||
}
|
||||
|
||||
/* @internal */
|
||||
|
||||
Reference in New Issue
Block a user