-
Notifications
You must be signed in to change notification settings - Fork 12.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve best type matching for elementwise elaborations #57537
base: main
Are you sure you want to change the base?
Changes from 8 commits
03f2a4d
bcf7ad4
2c98406
c605f6b
e9d136a
04953e2
6732851
e295774
dc9d465
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,6 +95,7 @@ import { | |
ConstructorTypeNode, | ||
ConstructSignatureDeclaration, | ||
contains, | ||
containsNonPublicProperties, | ||
containsParseError, | ||
ContextFlags, | ||
copyEntries, | ||
|
@@ -20352,16 +20353,16 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
} | ||
|
||
function getBestMatchIndexedAccessTypeOrUndefined(source: Type, target: Type, nameType: Type) { | ||
const idx = getIndexedAccessTypeOrUndefined(target, nameType); | ||
if (idx) { | ||
return idx; | ||
} | ||
if (target.flags & TypeFlags.Union) { | ||
const best = getBestMatchingType(source, target as UnionType); | ||
const best = getBestMatchingType(source, target as UnionType, /*matchSingleOverlappy*/ false); | ||
if (best) { | ||
return getIndexedAccessTypeOrUndefined(best, nameType); | ||
} | ||
} | ||
const idx = getIndexedAccessTypeOrUndefined(target, nameType); | ||
if (idx) { | ||
return idx; | ||
} | ||
} | ||
|
||
function checkExpressionForMutableLocationWithContextualType(next: Expression, sourcePropType: Type) { | ||
|
@@ -21983,8 +21984,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
} | ||
} | ||
if (reportErrors) { | ||
// Elaborate only if we can find a best matching type in the target union | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed this |
||
const bestMatchingType = getBestMatchingType(source, target, isRelatedTo); | ||
// Elaborate only if we can find a best matching type in the target | ||
const bestMatchingType = getBestMatchingType(source, target, /*matchSingleOverlappy*/ true, isRelatedTo); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returning a single overlappy type is required here to make the non-elementwise union-based elaboration to elaborate about the "last" element in the union, see the "recovered" errors in this commit: e9d136a |
||
if (bestMatchingType) { | ||
isRelatedTo(source, bestMatchingType, RecursionFlags.Target, /*reportErrors*/ true, /*headMessage*/ undefined, intersectionState); | ||
} | ||
|
@@ -23711,12 +23712,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
return getPropertiesOfType(type).filter(targetProp => containsMissingType(getTypeOfSymbol(targetProp))); | ||
} | ||
|
||
function getBestMatchingType(source: Type, target: UnionOrIntersectionType, isRelatedTo = compareTypesAssignable) { | ||
function getBestMatchingType(source: Type, target: UnionOrIntersectionType, matchSingleOverlappy: boolean, isRelatedTo = compareTypesAssignable) { | ||
return findMatchingDiscriminantType(source, target, isRelatedTo) || | ||
findMatchingTypeReferenceOrTypeAliasReference(source, target) || | ||
findBestTypeForObjectLiteral(source, target) || | ||
findBestTypeForInvokable(source, target) || | ||
findMostOverlappyType(source, target); | ||
findMostOverlappyType(source, filterTypesForObjectLiteralMatch(source, target), /*matchSingle*/ matchSingleOverlappy); | ||
} | ||
|
||
function discriminateTypeByDiscriminableItems(target: UnionType, discriminators: (readonly [() => Type, __String])[], related: (source: Type, target: Type) => boolean | Ternary) { | ||
|
@@ -50901,10 +50901,18 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
} | ||
} | ||
|
||
function findBestTypeForObjectLiteral(source: Type, unionTarget: UnionOrIntersectionType) { | ||
if (getObjectFlags(source) & ObjectFlags.ObjectLiteral && someType(unionTarget, isArrayLikeType)) { | ||
return find(unionTarget.types, t => !isArrayLikeType(t)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The referenced bug was a somewhat funny one because optional properties always include |
||
function filterTypesForObjectLiteralMatch(source: Type, target: UnionOrIntersectionType) { | ||
if (getObjectFlags(source) & ObjectFlags.ObjectLiteral && target.flags & TypeFlags.Union) { | ||
const filtered = filterType(target, t => | ||
!(t.flags & TypeFlags.Primitive) && | ||
!isArrayLikeType(t) && | ||
!typeHasCallOrConstructSignatures(t) && | ||
(everyContainedType(t, t => !t.symbol || !(t.symbol.flags & SymbolFlags.Class)) || !containsNonPublicProperties(getAugmentedPropertiesOfType(t)))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is quite related to #56183 - although completions and contextual types are orthogonal to elementwise elaborations. I only figured out that I should filter those nominal types here because I was working on that other PR in the past |
||
if (!(filtered.flags & TypeFlags.Never)) { | ||
return filtered; | ||
} | ||
} | ||
return target; | ||
} | ||
|
||
function findBestTypeForInvokable(source: Type, unionTarget: UnionOrIntersectionType) { | ||
|
@@ -50916,31 +50924,37 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
} | ||
} | ||
|
||
function findMostOverlappyType(source: Type, unionTarget: UnionOrIntersectionType) { | ||
let bestMatch: Type | undefined; | ||
function findMostOverlappyType(source: Type, target: Type, matchSingle: boolean) { | ||
if (!(target.flags & TypeFlags.UnionOrIntersection)) { | ||
return target; | ||
} | ||
let bestMatches: Type[] = []; | ||
if (!(source.flags & (TypeFlags.Primitive | TypeFlags.InstantiablePrimitive))) { | ||
let matchingCount = 0; | ||
for (const target of unionTarget.types) { | ||
if (!(target.flags & (TypeFlags.Primitive | TypeFlags.InstantiablePrimitive))) { | ||
const overlap = getIntersectionType([getIndexType(source), getIndexType(target)]); | ||
for (const type of (target as UnionOrIntersectionType).types) { | ||
if (!(type.flags & (TypeFlags.Primitive | TypeFlags.InstantiablePrimitive))) { | ||
const overlap = getIntersectionType([getIndexType(source), getIndexType(type)]); | ||
if (overlap.flags & TypeFlags.Index) { | ||
// perfect overlap of keys | ||
return target; | ||
return type; | ||
} | ||
else if (isUnitType(overlap) || overlap.flags & TypeFlags.Union) { | ||
// We only want to account for literal types otherwise. | ||
// If we have a union of index types, it seems likely that we | ||
// needed to elaborate between two generic mapped types anyway. | ||
const len = overlap.flags & TypeFlags.Union ? countWhere((overlap as UnionType).types, isUnitType) : 1; | ||
if (len >= matchingCount) { | ||
bestMatch = target; | ||
if (len > matchingCount || matchSingle) { | ||
bestMatches.length = 0; | ||
} | ||
bestMatches = append(bestMatches, type); | ||
matchingCount = len; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return bestMatch; | ||
return bestMatches.length ? getUnionType(bestMatches) : undefined; | ||
} | ||
|
||
function filterPrimitivesIfContainsNonPrimitive(type: UnionType) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
elementWiseErrorInUnionTarget1.ts(19,5): error TS2322: Type 'number' is not assignable to type 'string'. | ||
|
||
|
||
==== elementWiseErrorInUnionTarget1.ts (1 errors) ==== | ||
type SingleOrArray<T> = T | readonly T[]; | ||
|
||
type ProvidedActor = { | ||
src: string; | ||
input?: unknown; | ||
}; | ||
|
||
type MachineConfig<TActors extends ProvidedActor> = { | ||
invoke: SingleOrArray<TActors>; | ||
}; | ||
|
||
declare function setup<TActors extends ProvidedActor>(): { | ||
createMachine: (config: MachineConfig<TActors>) => void; | ||
}; | ||
|
||
setup<{ src: "fetchUser"; input: string }>().createMachine({ | ||
invoke: { | ||
src: "fetchUser", | ||
input: 10, | ||
~~~~~ | ||
!!! error TS2322: Type 'number' is not assignable to type 'string'. | ||
}, | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
//// [tests/cases/compiler/elementWiseErrorInUnionTarget1.ts] //// | ||
|
||
=== elementWiseErrorInUnionTarget1.ts === | ||
type SingleOrArray<T> = T | readonly T[]; | ||
>SingleOrArray : Symbol(SingleOrArray, Decl(elementWiseErrorInUnionTarget1.ts, 0, 0)) | ||
>T : Symbol(T, Decl(elementWiseErrorInUnionTarget1.ts, 0, 19)) | ||
>T : Symbol(T, Decl(elementWiseErrorInUnionTarget1.ts, 0, 19)) | ||
>T : Symbol(T, Decl(elementWiseErrorInUnionTarget1.ts, 0, 19)) | ||
|
||
type ProvidedActor = { | ||
>ProvidedActor : Symbol(ProvidedActor, Decl(elementWiseErrorInUnionTarget1.ts, 0, 41)) | ||
|
||
src: string; | ||
>src : Symbol(src, Decl(elementWiseErrorInUnionTarget1.ts, 2, 22)) | ||
|
||
input?: unknown; | ||
>input : Symbol(input, Decl(elementWiseErrorInUnionTarget1.ts, 3, 14)) | ||
|
||
}; | ||
|
||
type MachineConfig<TActors extends ProvidedActor> = { | ||
>MachineConfig : Symbol(MachineConfig, Decl(elementWiseErrorInUnionTarget1.ts, 5, 2)) | ||
>TActors : Symbol(TActors, Decl(elementWiseErrorInUnionTarget1.ts, 7, 19)) | ||
>ProvidedActor : Symbol(ProvidedActor, Decl(elementWiseErrorInUnionTarget1.ts, 0, 41)) | ||
|
||
invoke: SingleOrArray<TActors>; | ||
>invoke : Symbol(invoke, Decl(elementWiseErrorInUnionTarget1.ts, 7, 53)) | ||
>SingleOrArray : Symbol(SingleOrArray, Decl(elementWiseErrorInUnionTarget1.ts, 0, 0)) | ||
>TActors : Symbol(TActors, Decl(elementWiseErrorInUnionTarget1.ts, 7, 19)) | ||
|
||
}; | ||
|
||
declare function setup<TActors extends ProvidedActor>(): { | ||
>setup : Symbol(setup, Decl(elementWiseErrorInUnionTarget1.ts, 9, 2)) | ||
>TActors : Symbol(TActors, Decl(elementWiseErrorInUnionTarget1.ts, 11, 23)) | ||
>ProvidedActor : Symbol(ProvidedActor, Decl(elementWiseErrorInUnionTarget1.ts, 0, 41)) | ||
|
||
createMachine: (config: MachineConfig<TActors>) => void; | ||
>createMachine : Symbol(createMachine, Decl(elementWiseErrorInUnionTarget1.ts, 11, 58)) | ||
>config : Symbol(config, Decl(elementWiseErrorInUnionTarget1.ts, 12, 18)) | ||
>MachineConfig : Symbol(MachineConfig, Decl(elementWiseErrorInUnionTarget1.ts, 5, 2)) | ||
>TActors : Symbol(TActors, Decl(elementWiseErrorInUnionTarget1.ts, 11, 23)) | ||
|
||
}; | ||
|
||
setup<{ src: "fetchUser"; input: string }>().createMachine({ | ||
>setup<{ src: "fetchUser"; input: string }>().createMachine : Symbol(createMachine, Decl(elementWiseErrorInUnionTarget1.ts, 11, 58)) | ||
>setup : Symbol(setup, Decl(elementWiseErrorInUnionTarget1.ts, 9, 2)) | ||
>src : Symbol(src, Decl(elementWiseErrorInUnionTarget1.ts, 15, 7)) | ||
>input : Symbol(input, Decl(elementWiseErrorInUnionTarget1.ts, 15, 25)) | ||
>createMachine : Symbol(createMachine, Decl(elementWiseErrorInUnionTarget1.ts, 11, 58)) | ||
|
||
invoke: { | ||
>invoke : Symbol(invoke, Decl(elementWiseErrorInUnionTarget1.ts, 15, 60)) | ||
|
||
src: "fetchUser", | ||
>src : Symbol(src, Decl(elementWiseErrorInUnionTarget1.ts, 16, 11)) | ||
|
||
input: 10, | ||
>input : Symbol(input, Decl(elementWiseErrorInUnionTarget1.ts, 17, 21)) | ||
|
||
}, | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
//// [tests/cases/compiler/elementWiseErrorInUnionTarget1.ts] //// | ||
|
||
=== elementWiseErrorInUnionTarget1.ts === | ||
type SingleOrArray<T> = T | readonly T[]; | ||
>SingleOrArray : SingleOrArray<T> | ||
|
||
type ProvidedActor = { | ||
>ProvidedActor : { src: string; input?: unknown; } | ||
|
||
src: string; | ||
>src : string | ||
|
||
input?: unknown; | ||
>input : unknown | ||
|
||
}; | ||
|
||
type MachineConfig<TActors extends ProvidedActor> = { | ||
>MachineConfig : MachineConfig<TActors> | ||
|
||
invoke: SingleOrArray<TActors>; | ||
>invoke : SingleOrArray<TActors> | ||
|
||
}; | ||
|
||
declare function setup<TActors extends ProvidedActor>(): { | ||
>setup : <TActors extends ProvidedActor>() => { createMachine: (config: MachineConfig<TActors>) => void; } | ||
|
||
createMachine: (config: MachineConfig<TActors>) => void; | ||
>createMachine : (config: MachineConfig<TActors>) => void | ||
>config : MachineConfig<TActors> | ||
|
||
}; | ||
|
||
setup<{ src: "fetchUser"; input: string }>().createMachine({ | ||
>setup<{ src: "fetchUser"; input: string }>().createMachine({ invoke: { src: "fetchUser", input: 10, },}) : void | ||
>setup<{ src: "fetchUser"; input: string }>().createMachine : (config: MachineConfig<{ src: "fetchUser"; input: string; }>) => void | ||
>setup<{ src: "fetchUser"; input: string }>() : { createMachine: (config: MachineConfig<{ src: "fetchUser"; input: string; }>) => void; } | ||
>setup : <TActors extends ProvidedActor>() => { createMachine: (config: MachineConfig<TActors>) => void; } | ||
>src : "fetchUser" | ||
>input : string | ||
>createMachine : (config: MachineConfig<{ src: "fetchUser"; input: string; }>) => void | ||
>{ invoke: { src: "fetchUser", input: 10, },} : { invoke: { src: "fetchUser"; input: number; }; } | ||
|
||
invoke: { | ||
>invoke : { src: "fetchUser"; input: number; } | ||
>{ src: "fetchUser", input: 10, } : { src: "fetchUser"; input: number; } | ||
|
||
src: "fetchUser", | ||
>src : "fetchUser" | ||
>"fetchUser" : "fetchUser" | ||
|
||
input: 10, | ||
>input : number | ||
>10 : 10 | ||
|
||
}, | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
swapping the order of operations here improves some errors quite a bit in my eyes since the union discrimination happens early, for example: