Skip to content

Commit 9bf09ef

Browse files
committed
Instantiate an instantiation expression with the type arguments given
An instantiation expression whose type arguments did not satisfy their constraints kept the generic signature, unlike a type reference, whose type is that of the type arguments given, with the constraints checked separately. Checking the constraints while computing the type made both depend on order: in declare function b<U extends A = A>(): U; interface A<T = number> { value: T; child: typeof b<A<T>>; } checking A<T> against the constraint A needs the variances of A, which need the type of child. Checking A first measured the variances with child incomplete, which missed the constraint error, while querying child first reported it and kept the generic signature, giving child a different type. The signature is now instantiated with the type arguments given, as for a type reference, and the constraints are checked once the types they need are resolved, so child has the same type and error in every order. Type argument completions in a decorator read the signatures of its expression, which now has the type arguments being typed applied, so they read those of the expression the type arguments apply to. Fixes 61982 For 52892
1 parent 804869d commit 9bf09ef

5 files changed

Lines changed: 44 additions & 8 deletions

‎tsc/internal/checker/checker.go‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11117,11 +11117,15 @@ func (c *Checker) getInstantiationExpressionType(exprType *Type, node *ast.Node)
1111711117
return len(sig.TypeParameters()) != 0 && c.hasCorrectTypeArgumentArity(sig, typeArguments.Nodes)
1111811118
})
1111911119
return core.SameMap(applicableSignatures, func(sig *Signature) *Signature {
11120-
typeArgumentTypes := c.checkTypeArguments(sig, typeArguments.Nodes, true /*reportErrors*/, nil)
11121-
if typeArgumentTypes != nil {
11122-
return c.getSignatureInstantiation(sig, typeArgumentTypes, ast.IsInJSFile(sig.declaration), nil)
11123-
}
11124-
return sig
11120+
// As for a type reference, the type arguments are those given
11121+
// whether or not they satisfy their constraints, which are
11122+
// checked once the types they need are resolved: checking them
11123+
// can need the variances of a type whose members include this
11124+
// expression.
11125+
c.addDeferredDiagnostic(func() {
11126+
c.checkTypeArguments(sig, typeArguments.Nodes, true /*reportErrors*/, nil)
11127+
})
11128+
return c.getSignatureInstantiation(sig, core.Map(typeArguments.Nodes, c.getTypeFromTypeNode), ast.IsInJSFile(sig.declaration), nil)
1112511129
})
1112611130
}
1112711131
var getInstantiatedType func(*Type) *Type

‎tsc/internal/checker/services.go‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,12 @@ func (c *Checker) GetTypeArgumentConstraint(node *ast.Node) *Type {
706706
func (c *Checker) getUninstantiatedSignatures(node *ast.Node) []*Signature {
707707
switch node.Kind {
708708
case ast.KindCallExpression, ast.KindDecorator:
709-
return c.getSignaturesOfType(c.getTypeOfExpression(node.Expression()), SignatureKindCall)
709+
expression := node.Expression()
710+
if ast.IsDecorator(node) && ast.IsExpressionWithTypeArguments(expression) {
711+
// The type arguments of a decorator are part of its expression.
712+
expression = expression.Expression()
713+
}
714+
return c.getSignaturesOfType(c.getTypeOfExpression(expression), SignatureKindCall)
710715
case ast.KindNewExpression:
711716
return c.getSignaturesOfType(c.getTypeOfExpression(node.Expression()), SignatureKindConstruct)
712717
case ast.KindJsxSelfClosingElement, ast.KindJsxOpeningElement:

‎tsc/testdata/baselines/reference/compiler/instantiationExpressionConstraintCycleCheckOrder(checkorder=default).errors.txt‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
instantiationExpressionConstraintCycleCheckOrder.ts(10,25): error TS2344: Type 'A<T>' does not satisfy the constraint 'A<number>'.
2+
Type 'T' is not assignable to type 'number'.
13
instantiationExpressionConstraintCycleCheckOrder.ts(17,41): error TS2344: Type 'A<T>' does not satisfy the constraint 'A<number>'.
24
Type 'T' is not assignable to type 'number'.
35

46

5-
==== instantiationExpressionConstraintCycleCheckOrder.ts (1 errors) ====
7+
==== instantiationExpressionConstraintCycleCheckOrder.ts (2 errors) ====
68
// Checking the type arguments of the instantiation expressions against their
79
// constraints needs the variances of A, which need the type of child. The
810
// constraint errors and the types of child must not depend on whether child
@@ -13,6 +15,10 @@ instantiationExpressionConstraintCycleCheckOrder.ts(17,41): error TS2344: Type '
1315
interface A<T = number> {
1416
value: T;
1517
child: typeof b<A<T>>;
18+
~~~~
19+
!!! error TS2344: Type 'A<T>' does not satisfy the constraint 'A<number>'.
20+
!!! error TS2344: Type 'T' is not assignable to type 'number'.
21+
!!! related TS2208 instantiationExpressionConstraintCycleCheckOrder.ts:8:17: This type parameter might need an `extends number` constraint.
1622
}
1723
}
1824

‎tsc/testdata/baselines/reference/compiler/instantiationExpressionConstraintCycleCheckOrder(checkorder=typesfirst).types‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace InterfaceCycle {
1717
>value : T
1818

1919
child: typeof b<A<T>>;
20-
>child : () => A<T>
20+
>child : typeof b<A<T>>
2121
>b : <U extends A = A<number>>() => U
2222
}
2323
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
instantiationExpressionConstraintInStaticClassExpression.ts(6,35): error TS2344: Type 'A<T>' does not satisfy the constraint 'A<number>'.
2+
Type 'T' is not assignable to type 'number'.
3+
4+
5+
==== instantiationExpressionConstraintInStaticClassExpression.ts (1 errors) ====
6+
// A<T> does not satisfy the constraint A of B, as T is not assignable to
7+
// number, which must be reported.
8+
9+
class A<T = number> {
10+
value!: T;
11+
child!: InstanceType<typeof A.B<A<T>>>; // this should error
12+
~~~~
13+
!!! error TS2344: Type 'A<T>' does not satisfy the constraint 'A<number>'.
14+
!!! error TS2344: Type 'T' is not assignable to type 'number'.
15+
!!! related TS2208 instantiationExpressionConstraintInStaticClassExpression.ts:4:9: This type parameter might need an `extends number` constraint.
16+
17+
static B = class B<T extends A = A> {
18+
parent!: T;
19+
};
20+
}
21+

0 commit comments

Comments
 (0)