Skip to content

Commit f7feca5

Browse files
committed
Fixed the contextual params assignability with target params declared using union of tuples as its rest
1 parent bd712ab commit f7feca5

5 files changed

+40
-1
lines changed

src/compiler/checker.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -19383,7 +19383,24 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1938319383
const sourceType = i === restIndex ? getRestTypeAtPosition(source, i) : tryGetTypeAtPosition(source, i);
1938419384
let targetType = i === restIndex ? getRestTypeAtPosition(target, i) : tryGetTypeAtPosition(target, i);
1938519385
if (i === restIndex && targetType && sourceType && isTupleType(sourceType) && !sourceType.target.hasRestElement) {
19386-
targetType = mapType(targetType, t => isTupleType(t) && !t.target.hasRestElement ? sliceTupleType(t, 0, t.target.fixedLength - sourceType.target.fixedLength) : t);
19386+
targetType = mapType(targetType, t => {
19387+
if (!isTupleType(t)) {
19388+
return t;
19389+
}
19390+
19391+
const typeArguments = getTypeArguments(t);
19392+
const elementTypes: Type[] = [];
19393+
19394+
for (let i = 0; i < getTypeReferenceArity(sourceType); i++) {
19395+
elementTypes.push(
19396+
t.target.elementFlags[i] & ElementFlags.Required
19397+
? typeArguments[i]
19398+
: getElementTypeOfSliceOfTupleType(t, i)!
19399+
);
19400+
}
19401+
19402+
return createTupleType(elementTypes, elementTypes.map(() => ElementFlags.Required));
19403+
});
1938719404
}
1938819405
if (sourceType && targetType) {
1938919406
// In order to ensure that any generic type Foo<T> is at least co-variant with respect to T no matter
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/restTupleUnionWithRestContextualParams.ts ===
2+
const f1: (...args: [number, string, ...boolean[]] | [string, number, ...boolean[]]) => void = (a, b, c) => {};
3+
>f1 : Symbol(f1, Decl(restTupleUnionWithRestContextualParams.ts, 0, 5))
4+
>args : Symbol(args, Decl(restTupleUnionWithRestContextualParams.ts, 0, 11))
5+
>a : Symbol(a, Decl(restTupleUnionWithRestContextualParams.ts, 0, 96))
6+
>b : Symbol(b, Decl(restTupleUnionWithRestContextualParams.ts, 0, 98))
7+
>c : Symbol(c, Decl(restTupleUnionWithRestContextualParams.ts, 0, 101))
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
=== tests/cases/compiler/restTupleUnionWithRestContextualParams.ts ===
2+
const f1: (...args: [number, string, ...boolean[]] | [string, number, ...boolean[]]) => void = (a, b, c) => {};
3+
>f1 : (...args: [number, string, ...boolean[]] | [string, number, ...boolean[]]) => void
4+
>args : [number, string, ...boolean[]] | [string, number, ...boolean[]]
5+
>(a, b, c) => {} : (a: string | number, b: string | number, c: boolean) => void
6+
>a : string | number
7+
>b : string | number
8+
>c : boolean
9+

tests/cases/compiler/restTupleUnionShorterContextualParams.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @strict: true
12
// @noEmit: true
23

34
// repro #48663
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @strict: true
2+
// @noEmit: true
3+
4+
const f1: (...args: [number, string, ...boolean[]] | [string, number, ...boolean[]]) => void = (a, b, c) => {};

0 commit comments

Comments
 (0)