Skip to content

Commit 22fbb8e

Browse files
authored
Merge pull request microsoft#21744 from Microsoft/fixGetConstrainedTypeParameter
Fix getConstrainedTypeParameter function
2 parents c9ccf72 + a7437fd commit 22fbb8e

7 files changed

Lines changed: 58 additions & 2 deletions

File tree

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7245,7 +7245,7 @@ namespace ts {
72457245

72467246
function getConstrainedTypeParameter(typeParameter: TypeParameter, node: Node) {
72477247
let constraints: Type[];
7248-
while (isTypeNode(node)) {
7248+
while (isPartOfTypeNode(node)) {
72497249
const parent = node.parent;
72507250
if (parent.kind === SyntaxKind.ConditionalType && node === (<ConditionalTypeNode>parent).trueType) {
72517251
if (getTypeFromTypeNode((<ConditionalTypeNode>parent).checkType) === typeParameter) {

src/compiler/utilities.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,8 @@ namespace ts {
771771
return node.parent.kind !== SyntaxKind.VoidExpression;
772772
case SyntaxKind.ExpressionWithTypeArguments:
773773
return !isExpressionWithTypeArgumentsInClassExtendsClause(node);
774+
case SyntaxKind.TypeParameter:
775+
return node.parent.kind === SyntaxKind.MappedType || node.parent.kind === SyntaxKind.InferType;
774776

775777
// Identifiers and qualified names may be type nodes, depending on their context. Climb
776778
// above them to find the lowest container

tests/baselines/reference/inferTypes1.errors.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ tests/cases/conformance/types/conditional/inferTypes1.ts(72,43): error TS2304: C
1212
tests/cases/conformance/types/conditional/inferTypes1.ts(72,43): error TS4081: Exported type alias 'T62' has or is using private name 'U'.
1313
tests/cases/conformance/types/conditional/inferTypes1.ts(78,44): error TS2344: Type 'U' does not satisfy the constraint 'string'.
1414
Type 'number' is not assignable to type 'string'.
15+
tests/cases/conformance/types/conditional/inferTypes1.ts(131,40): error TS2322: Type 'T' is not assignable to type 'string'.
1516

1617

17-
==== tests/cases/conformance/types/conditional/inferTypes1.ts (12 errors) ====
18+
==== tests/cases/conformance/types/conditional/inferTypes1.ts (13 errors) ====
1819
type Unpacked<T> =
1920
T extends (infer U)[] ? U :
2021
T extends (...args: any[]) => infer U ? U :
@@ -167,4 +168,11 @@ tests/cases/conformance/types/conditional/inferTypes1.ts(78,44): error TS2344: T
167168
type A2<T, U extends void> = [T, U];
168169
type B2<S> = S extends A2<infer T, infer U> ? [T, U] : never;
169170
type C2<S, U extends void> = S extends A2<infer T, U> ? [T, U] : never;
171+
172+
// Repro from #21735
173+
174+
type A<T> = T extends string ? { [P in T]: void; } : T;
175+
type B<T> = string extends T ? { [P in T]: void; } : T; // Error
176+
~
177+
!!! error TS2322: Type 'T' is not assignable to type 'string'.
170178

tests/baselines/reference/inferTypes1.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ type B1<S> = S extends A1<infer T, infer U> ? [T, U] : never;
125125
type A2<T, U extends void> = [T, U];
126126
type B2<S> = S extends A2<infer T, infer U> ? [T, U] : never;
127127
type C2<S, U extends void> = S extends A2<infer T, U> ? [T, U] : never;
128+
129+
// Repro from #21735
130+
131+
type A<T> = T extends string ? { [P in T]: void; } : T;
132+
type B<T> = string extends T ? { [P in T]: void; } : T; // Error
128133

129134

130135
//// [inferTypes1.js]

tests/baselines/reference/inferTypes1.symbols

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,3 +551,21 @@ type C2<S, U extends void> = S extends A2<infer T, U> ? [T, U] : never;
551551
>T : Symbol(T, Decl(inferTypes1.ts, 125, 47))
552552
>U : Symbol(U, Decl(inferTypes1.ts, 125, 10))
553553

554+
// Repro from #21735
555+
556+
type A<T> = T extends string ? { [P in T]: void; } : T;
557+
>A : Symbol(A, Decl(inferTypes1.ts, 125, 71))
558+
>T : Symbol(T, Decl(inferTypes1.ts, 129, 7))
559+
>T : Symbol(T, Decl(inferTypes1.ts, 129, 7))
560+
>P : Symbol(P, Decl(inferTypes1.ts, 129, 34))
561+
>T : Symbol(T, Decl(inferTypes1.ts, 129, 7))
562+
>T : Symbol(T, Decl(inferTypes1.ts, 129, 7))
563+
564+
type B<T> = string extends T ? { [P in T]: void; } : T; // Error
565+
>B : Symbol(B, Decl(inferTypes1.ts, 129, 55))
566+
>T : Symbol(T, Decl(inferTypes1.ts, 130, 7))
567+
>T : Symbol(T, Decl(inferTypes1.ts, 130, 7))
568+
>P : Symbol(P, Decl(inferTypes1.ts, 130, 34))
569+
>T : Symbol(T, Decl(inferTypes1.ts, 130, 7))
570+
>T : Symbol(T, Decl(inferTypes1.ts, 130, 7))
571+

tests/baselines/reference/inferTypes1.types

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,3 +558,21 @@ type C2<S, U extends void> = S extends A2<infer T, U> ? [T, U] : never;
558558
>T : T
559559
>U : U
560560

561+
// Repro from #21735
562+
563+
type A<T> = T extends string ? { [P in T]: void; } : T;
564+
>A : A<T>
565+
>T : T
566+
>T : T
567+
>P : P
568+
>T : T
569+
>T : T
570+
571+
type B<T> = string extends T ? { [P in T]: void; } : T; // Error
572+
>B : B<T>
573+
>T : T
574+
>T : T
575+
>P : P
576+
>T : T
577+
>T : T
578+

tests/cases/conformance/types/conditional/inferTypes1.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,8 @@ type B1<S> = S extends A1<infer T, infer U> ? [T, U] : never;
127127
type A2<T, U extends void> = [T, U];
128128
type B2<S> = S extends A2<infer T, infer U> ? [T, U] : never;
129129
type C2<S, U extends void> = S extends A2<infer T, U> ? [T, U] : never;
130+
131+
// Repro from #21735
132+
133+
type A<T> = T extends string ? { [P in T]: void; } : T;
134+
type B<T> = string extends T ? { [P in T]: void; } : T; // Error

0 commit comments

Comments
 (0)