Skip to content

Commit 804869d

Browse files
committed
Infer the constraint of an infer type parameter without instantiating
The constraint of U in a type reference such as S<infer U> is inferred from the constraint of the corresponding type parameter of S. Finding that type parameter resolved the type of the reference, instantiating S with U, which can itself need the constraint of U. In type Copy<T> = { [K in keyof T]: T[K] }; type S<T extends object> = Copy<{ x: 0 } & T>; type I = {} extends S<infer U> ? U : never; U had no constraint, and did not satisfy object, when the type arguments of S<infer U> were checked first, but had the constraint object when I was queried first. The type parameters of the declaration the reference refers to are now used directly. Fixes 38460
1 parent 2829c07 commit 804869d

2 files changed

Lines changed: 27 additions & 17 deletions

File tree

‎tsc/internal/checker/checker.go‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17997,7 +17997,7 @@ func (c *Checker) getInferredTypeParameterConstraint(t *Type, omitTypeReferences
1799717997
}
1799817998
switch {
1799917999
case ast.IsTypeReferenceNode(parent) && !omitTypeReferences:
18000-
typeParameters := c.getTypeParametersForTypeReferenceOrImport(parent)
18000+
typeParameters := c.getDeclaredTypeParametersOfTypeReference(parent)
1800118001
if typeParameters != nil {
1800218002
index := slices.Index(parent.TypeArguments(), child)
1800318003
if index >= 0 && index < len(typeParameters) {
@@ -18064,6 +18064,32 @@ func (c *Checker) getTypeParametersForTypeReferenceOrImport(node *ast.Node) []*T
1806418064
return nil
1806518065
}
1806618066

18067+
// getDeclaredTypeParametersOfTypeReference returns the type parameters of the
18068+
// type a type reference refers to, if it has an acceptable number of type
18069+
// arguments. Unlike getTypeParametersForTypeReferenceOrImport, it does not
18070+
// instantiate the type, which can need the constraints of the infer type
18071+
// parameters among its type arguments, inferred from these type parameters.
18072+
func (c *Checker) getDeclaredTypeParametersOfTypeReference(node *ast.Node) []*Type {
18073+
symbol := c.getSymbolFromTypeReference(node)
18074+
if symbol == c.unknownSymbol {
18075+
return nil
18076+
}
18077+
declared := c.getDeclaredTypeOfSymbol(symbol)
18078+
if c.isErrorType(declared) {
18079+
return nil
18080+
}
18081+
var typeParameters []*Type
18082+
if symbol.Flags()&ast.SymbolFlagsTypeAlias != 0 {
18083+
typeParameters = c.typeAliasLinks.Get(symbol).typeParameters
18084+
} else if declared.objectFlags&ObjectFlagsClassOrInterface != 0 {
18085+
typeParameters = declared.AsInterfaceType().LocalTypeParameters()
18086+
}
18087+
if n := len(node.TypeArguments()); n < c.getMinTypeArgumentCount(typeParameters) || n > len(typeParameters) {
18088+
return nil
18089+
}
18090+
return typeParameters
18091+
}
18092+
1806718093
func (c *Checker) getTypeParametersForTypeAndSymbol(t *Type, symbol *ast.Symbol) []*Type {
1806818094
if !c.isErrorType(t) {
1806918095
if symbol.Flags()&ast.SymbolFlagsTypeAlias != 0 {

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

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)