Merge pull request #21744 from Microsoft/fixGetConstrainedTypeParameter

Fix getConstrainedTypeParameter function
This commit is contained in:
Anders Hejlsberg 2018-02-07 16:37:10 -08:00 committed by GitHub
commit 22fbb8edce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 58 additions and 2 deletions

View file

@ -7245,7 +7245,7 @@ namespace ts {
function getConstrainedTypeParameter(typeParameter: TypeParameter, node: Node) {
let constraints: Type[];
while (isTypeNode(node)) {
while (isPartOfTypeNode(node)) {
const parent = node.parent;
if (parent.kind === SyntaxKind.ConditionalType && node === (<ConditionalTypeNode>parent).trueType) {
if (getTypeFromTypeNode((<ConditionalTypeNode>parent).checkType) === typeParameter) {

View file

@ -771,6 +771,8 @@ namespace ts {
return node.parent.kind !== SyntaxKind.VoidExpression;
case SyntaxKind.ExpressionWithTypeArguments:
return !isExpressionWithTypeArgumentsInClassExtendsClause(node);
case SyntaxKind.TypeParameter:
return node.parent.kind === SyntaxKind.MappedType || node.parent.kind === SyntaxKind.InferType;
// Identifiers and qualified names may be type nodes, depending on their context. Climb
// above them to find the lowest container

View file

@ -12,9 +12,10 @@ tests/cases/conformance/types/conditional/inferTypes1.ts(72,43): error TS2304: C
tests/cases/conformance/types/conditional/inferTypes1.ts(72,43): error TS4081: Exported type alias 'T62' has or is using private name 'U'.
tests/cases/conformance/types/conditional/inferTypes1.ts(78,44): error TS2344: Type 'U' does not satisfy the constraint 'string'.
Type 'number' is not assignable to type 'string'.
tests/cases/conformance/types/conditional/inferTypes1.ts(131,40): error TS2322: Type 'T' is not assignable to type 'string'.
==== tests/cases/conformance/types/conditional/inferTypes1.ts (12 errors) ====
==== tests/cases/conformance/types/conditional/inferTypes1.ts (13 errors) ====
type Unpacked<T> =
T extends (infer U)[] ? U :
T extends (...args: any[]) => infer U ? U :
@ -167,4 +168,11 @@ tests/cases/conformance/types/conditional/inferTypes1.ts(78,44): error TS2344: T
type A2<T, U extends void> = [T, U];
type B2<S> = S extends A2<infer T, infer U> ? [T, U] : never;
type C2<S, U extends void> = S extends A2<infer T, U> ? [T, U] : never;
// Repro from #21735
type A<T> = T extends string ? { [P in T]: void; } : T;
type B<T> = string extends T ? { [P in T]: void; } : T; // Error
~
!!! error TS2322: Type 'T' is not assignable to type 'string'.

View file

@ -125,6 +125,11 @@ type B1<S> = S extends A1<infer T, infer U> ? [T, U] : never;
type A2<T, U extends void> = [T, U];
type B2<S> = S extends A2<infer T, infer U> ? [T, U] : never;
type C2<S, U extends void> = S extends A2<infer T, U> ? [T, U] : never;
// Repro from #21735
type A<T> = T extends string ? { [P in T]: void; } : T;
type B<T> = string extends T ? { [P in T]: void; } : T; // Error
//// [inferTypes1.js]

View file

@ -551,3 +551,21 @@ type C2<S, U extends void> = S extends A2<infer T, U> ? [T, U] : never;
>T : Symbol(T, Decl(inferTypes1.ts, 125, 47))
>U : Symbol(U, Decl(inferTypes1.ts, 125, 10))
// Repro from #21735
type A<T> = T extends string ? { [P in T]: void; } : T;
>A : Symbol(A, Decl(inferTypes1.ts, 125, 71))
>T : Symbol(T, Decl(inferTypes1.ts, 129, 7))
>T : Symbol(T, Decl(inferTypes1.ts, 129, 7))
>P : Symbol(P, Decl(inferTypes1.ts, 129, 34))
>T : Symbol(T, Decl(inferTypes1.ts, 129, 7))
>T : Symbol(T, Decl(inferTypes1.ts, 129, 7))
type B<T> = string extends T ? { [P in T]: void; } : T; // Error
>B : Symbol(B, Decl(inferTypes1.ts, 129, 55))
>T : Symbol(T, Decl(inferTypes1.ts, 130, 7))
>T : Symbol(T, Decl(inferTypes1.ts, 130, 7))
>P : Symbol(P, Decl(inferTypes1.ts, 130, 34))
>T : Symbol(T, Decl(inferTypes1.ts, 130, 7))
>T : Symbol(T, Decl(inferTypes1.ts, 130, 7))

View file

@ -558,3 +558,21 @@ type C2<S, U extends void> = S extends A2<infer T, U> ? [T, U] : never;
>T : T
>U : U
// Repro from #21735
type A<T> = T extends string ? { [P in T]: void; } : T;
>A : A<T>
>T : T
>T : T
>P : P
>T : T
>T : T
type B<T> = string extends T ? { [P in T]: void; } : T; // Error
>B : B<T>
>T : T
>T : T
>P : P
>T : T
>T : T

View file

@ -127,3 +127,8 @@ type B1<S> = S extends A1<infer T, infer U> ? [T, U] : never;
type A2<T, U extends void> = [T, U];
type B2<S> = S extends A2<infer T, infer U> ? [T, U] : never;
type C2<S, U extends void> = S extends A2<infer T, U> ? [T, U] : never;
// Repro from #21735
type A<T> = T extends string ? { [P in T]: void; } : T;
type B<T> = string extends T ? { [P in T]: void; } : T; // Error