Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,6 @@ type InferenceContext struct {
type InferenceInfo struct {
typeParameter *Type // Type parameter for which inferences are being made
candidates []*Type // Candidates in covariant positions in decreasing depth order
candidateDepths []int // Type argument depths of covariant inferences
contraCandidates []*Type // Candidates in contravariant positions
inferredType *Type // Cache for resolved inferred type
priority InferencePriority // Priority of current inference set
Expand Down
97 changes: 65 additions & 32 deletions internal/checker/inference.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ type InferenceState struct {
sourceStack []*Type
targetStack []*Type
next *InferenceState
depth int
}

func (c *Checker) getInferenceState() *InferenceState {
Expand Down Expand Up @@ -108,11 +107,11 @@ func (c *Checker) inferFromTypes(n *InferenceState, source *Type, target *Type)
}
// First, infer between identically matching source and target constituents and remove the
// matching types.
tempSources, tempTargets := c.inferFromMatchingTypes(n, sourceTypes, target.Distributed(), (*Checker).isTypeOrBaseIdenticalTo)
tempSources, tempTargets := c.inferFromMatchingTypes(n, sourceTypes, target.Types(), (*Checker).isTypeOrBaseIdenticalTo, false /*sort*/)
// Next, infer between closely matching source and target constituents and remove
// the matching types. Types closely match when they are instantiations of the same
// object type or instantiations of the same type alias.
sources, targets := c.inferFromMatchingTypes(n, tempSources, tempTargets, (*Checker).isTypeCloselyMatchedBy)
sources, targets := c.inferFromMatchingTypes(n, tempSources, tempTargets, (*Checker).isTypeCloselyMatchedBy, true /*sort*/)
Comment on lines 108 to +114
if len(targets) == 0 {
return
}
Expand Down Expand Up @@ -140,7 +139,7 @@ func (c *Checker) inferFromTypes(n *InferenceState, source *Type, target *Type)
sourceTypes = []*Type{source}
}
// Infer between identically matching source and target constituents and remove the matching types.
sources, targets := c.inferFromMatchingTypes(n, sourceTypes, target.Types(), (*Checker).isTypeIdenticalTo)
sources, targets := c.inferFromMatchingTypes(n, sourceTypes, target.Types(), (*Checker).isTypeIdenticalTo, false /*sort*/)
if len(sources) == 0 || len(targets) == 0 {
return
}
Expand Down Expand Up @@ -188,7 +187,6 @@ func (c *Checker) inferFromTypes(n *InferenceState, source *Type, target *Type)
}
if n.priority < inference.priority {
inference.candidates = nil
inference.candidateDepths = nil
inference.contraCandidates = nil
inference.topLevel = true
inference.priority = n.priority
Expand All @@ -201,28 +199,9 @@ func (c *Checker) inferFromTypes(n *InferenceState, source *Type, target *Type)
inference.contraCandidates = append(inference.contraCandidates, candidate)
clearCachedInferences(n.inferences)
}
} else {
index := slices.Index(inference.candidates, candidate)
if index < 0 || inference.candidateDepths[index] < n.depth {
// Candidate isn't present or is present with lower depth
if index >= 0 {
// Remove candidate with lower depth
inference.candidates = slices.Delete(inference.candidates, index, index+1)
inference.candidateDepths = slices.Delete(inference.candidateDepths, index, index+1)
}
index = 0
for index < len(inference.candidateDepths) {
if inference.candidateDepths[index] < n.depth {
break
}
index++
}
// Insert candidate at end or immediately before first candidate with lower depth.
// This ensures candidates with the highest depth are stored first.
inference.candidates = slices.Insert(inference.candidates, index, candidate)
inference.candidateDepths = slices.Insert(inference.candidateDepths, index, n.depth)
clearCachedInferences(n.inferences)
}
} else if !slices.Contains(inference.candidates, candidate) {
inference.candidates = append(inference.candidates, candidate)
clearCachedInferences(n.inferences)
}
}
if n.priority&InferencePriorityReturnType == 0 && target.flags&TypeFlagsTypeParameter != 0 && inference.topLevel && !c.isTypeParameterAtTopLevel(n.originalTarget, target, 0) {
Expand Down Expand Up @@ -303,15 +282,13 @@ func (c *Checker) inferFromTypes(n *InferenceState, source *Type, target *Type)
}

func (c *Checker) inferFromTypeArguments(n *InferenceState, sourceTypes []*Type, targetTypes []*Type, variances []VarianceFlags) {
n.depth++
for i := range min(len(sourceTypes), len(targetTypes)) {
if i < len(variances) && variances[i]&VarianceFlagsVarianceMask == VarianceFlagsContravariant {
c.inferFromContravariantTypes(n, sourceTypes[i], targetTypes[i])
} else {
c.inferFromTypes(n, sourceTypes[i], targetTypes[i])
}
}
n.depth--
}

func (c *Checker) inferWithPriority(n *InferenceState, source *Type, target *Type, newPriority InferencePriority) {
Expand Down Expand Up @@ -390,18 +367,36 @@ func (c *Checker) invokeOnce(n *InferenceState, source *Type, target *Type, acti
n.inferencePriority = min(n.inferencePriority, saveInferencePriority)
}

func (c *Checker) inferFromMatchingTypes(n *InferenceState, sources []*Type, targets []*Type, matches func(c *Checker, s *Type, t *Type) bool) ([]*Type, []*Type) {
func (c *Checker) inferFromMatchingTypes(n *InferenceState, sources []*Type, targets []*Type, matches func(c *Checker, s *Type, t *Type) bool, sort bool) ([]*Type, []*Type) {
var matchedSources []*Type
var matchedTargets []*Type
for _, t := range targets {
for _, s := range sources {
if matches(c, s, t) {
c.inferFromTypes(n, s, t)
if !sort {
c.inferFromTypes(n, s, t)
}
matchedSources = core.AppendIfUnique(matchedSources, s)
matchedTargets = core.AppendIfUnique(matchedTargets, t)
}
}
}
if sort {
// Sort target types by decreasing depth of generic instantiations. Intuitively, a successful
// inference from a type argument with deeper nesting is of higher quality because we've stripped
// away more layers of type instantiations that otherwise might skew the results. For example,
// when inferring from string[] | string[][] to T[] | T[][], the inference of string we make from
// relating string[][] to T[][] is of higher quality than the inference of string[] we make relating
// string[][] to T[].
slices.SortFunc(matchedTargets, compareTypesAndDepth)
for _, t := range matchedTargets {
for _, s := range matchedSources {
if matches(c, s, t) {
c.inferFromTypes(n, s, t)
}
}
}
}
if len(matchedSources) != 0 {
sources = core.Filter(sources, func(t *Type) bool { return !slices.Contains(matchedSources, t) })
}
Expand All @@ -411,6 +406,45 @@ func (c *Checker) inferFromMatchingTypes(n *InferenceState, sources []*Type, tar
return sources, targets
}

// Compare two types first by depth and then by the regular type ordering.
func compareTypesAndDepth(t1, t2 *Type) int {
d1 := getTypeDepth(t1, 3)
d2 := getTypeDepth(t2, 3)
if d1 != d2 {
return d2 - d1 // Largest depth sorts first
}
return CompareTypes(t1, t2)
}

// Return the depth of the given type up to the given maximum depth. For generic aliased types
// and type references, the depth is one plus the largest type argument depth. For union and
// intersection types, the depth is the largest constituent type depth. For all other types,
// the depth is zero. The maximum depth limits infinite recursion of circular types.
func getTypeDepth(t *Type, maxDepth int) int {
if maxDepth != 0 {
if t.alias != nil && len(t.alias.typeArguments) != 0 {
return getTypeListDepth(t.alias.typeArguments, maxDepth-1) + 1
}
if t.objectFlags&ObjectFlagsReference != 0 {
if typeArguments := t.checker.getTypeArguments(t); len(typeArguments) != 0 {
return getTypeListDepth(typeArguments, maxDepth-1) + 1
}
}
if t.flags&TypeFlagsUnionOrIntersection != 0 {
return getTypeListDepth(t.Types(), maxDepth)
}
}
return 0
}

func getTypeListDepth(types []*Type, maxDepth int) int {
depth := 0
for _, t := range types {
depth = max(depth, getTypeDepth(t, maxDepth))
}
return depth
}

func (c *Checker) inferToMultipleTypes(n *InferenceState, source *Type, targets []*Type, targetFlags TypeFlags) {
typeVariableCount := 0
if targetFlags&TypeFlagsUnion != 0 {
Expand Down Expand Up @@ -1596,7 +1630,6 @@ func cloneInferenceInfo(info *InferenceInfo) *InferenceInfo {
return &InferenceInfo{
typeParameter: info.typeParameter,
candidates: slices.Clone(info.candidates),
candidateDepths: slices.Clone(info.candidateDepths),
contraCandidates: slices.Clone(info.contraCandidates),
inferredType: info.inferredType,
priority: info.priority,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,32 @@ flat2(arg2);
>flat2 : Symbol(flat2, Decl(nestedGenericTypeInference.ts, 15, 12))
>arg2 : Symbol(arg2, Decl(nestedGenericTypeInference.ts, 18, 13))

// https://github.com/oxc-project/tsgolint/issues/1058

interface Column<T> {
>Column : Symbol(Column, Decl(nestedGenericTypeInference.ts, 19, 12))
>T : Symbol(T, Decl(nestedGenericTypeInference.ts, 23, 17))

dataIndex?: (T | (string & {}))[]
>dataIndex : Symbol(Column.dataIndex, Decl(nestedGenericTypeInference.ts, 23, 21))
>T : Symbol(T, Decl(nestedGenericTypeInference.ts, 23, 17))
}

declare function table<T>(rows: readonly T[], columns: Column<T>[]): void
>table : Symbol(table, Decl(nestedGenericTypeInference.ts, 25, 1))
>T : Symbol(T, Decl(nestedGenericTypeInference.ts, 27, 23))
>rows : Symbol(rows, Decl(nestedGenericTypeInference.ts, 27, 26))
>T : Symbol(T, Decl(nestedGenericTypeInference.ts, 27, 23))
>columns : Symbol(columns, Decl(nestedGenericTypeInference.ts, 27, 45))
>Column : Symbol(Column, Decl(nestedGenericTypeInference.ts, 19, 12))
>T : Symbol(T, Decl(nestedGenericTypeInference.ts, 27, 23))

declare const rows: { id: number }[]
>rows : Symbol(rows, Decl(nestedGenericTypeInference.ts, 29, 13))
>id : Symbol(id, Decl(nestedGenericTypeInference.ts, 29, 21))

table(rows, [{ dataIndex: ['id'] }])
>table : Symbol(table, Decl(nestedGenericTypeInference.ts, 25, 1))
>rows : Symbol(rows, Decl(nestedGenericTypeInference.ts, 29, 13))
>dataIndex : Symbol(dataIndex, Decl(nestedGenericTypeInference.ts, 31, 14))

Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,29 @@ flat2(arg2);
>flat2 : <T>(args: Box<T> | Box<Array<T>>) => void
>arg2 : Box<string> | Box<string[]>

// https://github.com/oxc-project/tsgolint/issues/1058

interface Column<T> {
dataIndex?: (T | (string & {}))[]
>dataIndex : (T | (string & {}))[] | undefined
}

declare function table<T>(rows: readonly T[], columns: Column<T>[]): void
>table : <T>(rows: readonly T[], columns: Column<T>[]) => void
>rows : readonly T[]
>columns : Column<T>[]

declare const rows: { id: number }[]
>rows : { id: number; }[]
>id : number

table(rows, [{ dataIndex: ['id'] }])
>table(rows, [{ dataIndex: ['id'] }]) : void
>table : <T>(rows: readonly T[], columns: Column<T>[]) => void
>rows : { id: number; }[]
>[{ dataIndex: ['id'] }] : { dataIndex: string[]; }[]
>{ dataIndex: ['id'] } : { dataIndex: string[]; }
>dataIndex : string[]
>['id'] : string[]
>'id' : "id"

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
assignmentCompatWithGenericCallSignatures2.ts(15,1): error TS2322: Type 'B' is not assignable to type 'A'.
Types of parameters 'x' and 'x' are incompatible.
Type 'T' is not assignable to type 'T[]'.
Types of parameters 'y' and 'y' are incompatible.
Type 'T[]' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to 'T[]'.
assignmentCompatWithGenericCallSignatures2.ts(16,1): error TS2322: Type 'A' is not assignable to type 'B'.
Types of parameters 'y' and 'y' are incompatible.
Type 'S' is not assignable to type 'S[]'.
Expand All @@ -24,9 +25,9 @@ assignmentCompatWithGenericCallSignatures2.ts(16,1): error TS2322: Type 'A' is n
a = b;
~
!!! error TS2322: Type 'B' is not assignable to type 'A'.
!!! error TS2322: Types of parameters 'x' and 'x' are incompatible.
!!! error TS2322: Type 'T' is not assignable to type 'T[]'.
!!! related TS2208 assignmentCompatWithGenericCallSignatures2.ts:4:6: This type parameter might need an `extends T[]` constraint.
!!! error TS2322: Types of parameters 'y' and 'y' are incompatible.
!!! error TS2322: Type 'T[]' is not assignable to type 'T'.
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'T[]'.
b = a;
~
!!! error TS2322: Type 'A' is not assignable to type 'B'.
Expand Down

This file was deleted.

12 changes: 12 additions & 0 deletions testdata/tests/cases/compiler/nestedGenericTypeInference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,15 @@ flat1(arg1);
declare function flat2<T>(args: Box<T> | Box<Array<T>>): void;
declare const arg2: Box<string> | Box<Array<string>>;
flat2(arg2);

// https://github.com/oxc-project/tsgolint/issues/1058

interface Column<T> {
dataIndex?: (T | (string & {}))[]
}

declare function table<T>(rows: readonly T[], columns: Column<T>[]): void

declare const rows: { id: number }[]

table(rows, [{ dataIndex: ['id'] }])