Skip to content

Commit 6554965

Browse files
committed
Fixed an issue with not being able to use mapped type over union constraint as rest param
1 parent aa2b235 commit 6554965

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

src/compiler/checker.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -12318,7 +12318,7 @@ namespace ts {
1231812318
const typeVariable = getHomomorphicTypeVariable(type);
1231912319
if (typeVariable && !type.declaration.nameType) {
1232012320
const constraint = getConstraintOfTypeParameter(typeVariable);
12321-
if (constraint && isArrayOrTupleType(constraint)) {
12321+
if (constraint && everyType(constraint, isArrayOrTupleType)) {
1232212322
return instantiateType(type, prependTypeMapping(typeVariable, constraint, type.mapper));
1232312323
}
1232412324
}
@@ -19860,7 +19860,7 @@ namespace ts {
1986019860
return varianceResult;
1986119861
}
1986219862
}
19863-
else if (isReadonlyArrayType(target) ? isArrayOrTupleType(source) : isArrayType(target) && isTupleType(source) && !source.target.readonly) {
19863+
else if (isReadonlyArrayType(target) ? everyType(source, isArrayOrTupleType) : isArrayType(target) && isTupleType(source) && !source.target.readonly) {
1986419864
if (relation !== identityRelation) {
1986519865
return isRelatedTo(getIndexTypeOfType(source, numberType) || anyType, getIndexTypeOfType(target, numberType) || anyType, RecursionFlags.Both, reportErrors);
1986619866
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
=== tests/cases/compiler/restParamUsingMappedTypeOverUnionConstraint.ts ===
2+
// repro 29919#issuecomment-470948453
3+
4+
type Mapped<T> = { [P in keyof T]: T[P] extends string ? [number] : [string] }
5+
>Mapped : Symbol(Mapped, Decl(restParamUsingMappedTypeOverUnionConstraint.ts, 0, 0))
6+
>T : Symbol(T, Decl(restParamUsingMappedTypeOverUnionConstraint.ts, 2, 12))
7+
>P : Symbol(P, Decl(restParamUsingMappedTypeOverUnionConstraint.ts, 2, 20))
8+
>T : Symbol(T, Decl(restParamUsingMappedTypeOverUnionConstraint.ts, 2, 12))
9+
>T : Symbol(T, Decl(restParamUsingMappedTypeOverUnionConstraint.ts, 2, 12))
10+
>P : Symbol(P, Decl(restParamUsingMappedTypeOverUnionConstraint.ts, 2, 20))
11+
12+
declare function test<T extends [number] | [string]>(
13+
>test : Symbol(test, Decl(restParamUsingMappedTypeOverUnionConstraint.ts, 2, 78))
14+
>T : Symbol(T, Decl(restParamUsingMappedTypeOverUnionConstraint.ts, 4, 22))
15+
16+
args: T,
17+
>args : Symbol(args, Decl(restParamUsingMappedTypeOverUnionConstraint.ts, 4, 53))
18+
>T : Symbol(T, Decl(restParamUsingMappedTypeOverUnionConstraint.ts, 4, 22))
19+
20+
fn: (...args: Mapped<T>) => void
21+
>fn : Symbol(fn, Decl(restParamUsingMappedTypeOverUnionConstraint.ts, 5, 10))
22+
>args : Symbol(args, Decl(restParamUsingMappedTypeOverUnionConstraint.ts, 6, 7))
23+
>Mapped : Symbol(Mapped, Decl(restParamUsingMappedTypeOverUnionConstraint.ts, 0, 0))
24+
>T : Symbol(T, Decl(restParamUsingMappedTypeOverUnionConstraint.ts, 4, 22))
25+
26+
): number
27+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/compiler/restParamUsingMappedTypeOverUnionConstraint.ts ===
2+
// repro 29919#issuecomment-470948453
3+
4+
type Mapped<T> = { [P in keyof T]: T[P] extends string ? [number] : [string] }
5+
>Mapped : Mapped<T>
6+
7+
declare function test<T extends [number] | [string]>(
8+
>test : <T extends [number] | [string]>(args: T, fn: (...args: Mapped<T>) => void) => number
9+
10+
args: T,
11+
>args : T
12+
13+
fn: (...args: Mapped<T>) => void
14+
>fn : (...args: Mapped<T>) => void
15+
>args : Mapped<T>
16+
17+
): number
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @noEmit: true
2+
// @strict: true
3+
4+
// repro 29919#issuecomment-470948453
5+
6+
type Mapped<T> = { [P in keyof T]: T[P] extends string ? [number] : [string] }
7+
8+
declare function test<T extends [number] | [string]>(
9+
args: T,
10+
fn: (...args: Mapped<T>) => void
11+
): number

0 commit comments

Comments
 (0)