Merge pull request #21744 from Microsoft/fixGetConstrainedTypeParameter
Fix getConstrainedTypeParameter function
This commit is contained in:
commit
22fbb8edce
7 changed files with 58 additions and 2 deletions
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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'.
|
||||
|
|
@ -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]
|
||||
|
|
|
@ -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))
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue