mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-12-13 04:57:55 -06:00
Generalize the fastpath for comparisons of unions which are correspondences (#41903)
* Generalize the fastpath for comparisons of unions which are correspondences to unions resulting from the application of intersections * Add comment
This commit is contained in:
parent
422fd1962e
commit
b9f372ed00
@ -17292,14 +17292,31 @@ namespace ts {
|
||||
return Ternary.False;
|
||||
}
|
||||
|
||||
function getUndefinedStrippedTargetIfNeeded(source: Type, target: Type) {
|
||||
// As a builtin type, `undefined` is a very low type ID - making it almsot always first, making this a very fast check to see
|
||||
// if we need to strip `undefined` from the target
|
||||
if (source.flags & TypeFlags.Union && target.flags & TypeFlags.Union &&
|
||||
!((source as UnionType).types[0].flags & TypeFlags.Undefined) && (target as UnionType).types[0].flags & TypeFlags.Undefined) {
|
||||
return extractTypesOfKind(target, ~TypeFlags.Undefined);
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
function eachTypeRelatedToType(source: UnionOrIntersectionType, target: Type, reportErrors: boolean, intersectionState: IntersectionState): Ternary {
|
||||
let result = Ternary.True;
|
||||
const sourceTypes = source.types;
|
||||
// We strip `undefined` from the target if the `source` trivially doesn't contain it for our correspondence-checking fastpath
|
||||
// since `undefined` is frequently added by optionality and would otherwise spoil a potentially useful correspondence
|
||||
const undefinedStrippedTarget = getUndefinedStrippedTargetIfNeeded(source, target as UnionType);
|
||||
for (let i = 0; i < sourceTypes.length; i++) {
|
||||
const sourceType = sourceTypes[i];
|
||||
if (target.flags & TypeFlags.Union && (target as UnionType).types.length === sourceTypes.length) {
|
||||
if (undefinedStrippedTarget.flags & TypeFlags.Union && sourceTypes.length >= (undefinedStrippedTarget as UnionType).types.length && sourceTypes.length % (undefinedStrippedTarget as UnionType).types.length === 0) {
|
||||
// many unions are mappings of one another; in such cases, simply comparing members at the same index can shortcut the comparison
|
||||
const related = isRelatedTo(sourceType, (target as UnionType).types[i], /*reportErrors*/ false, /*headMessage*/ undefined, intersectionState);
|
||||
// such unions will have identical lengths, and their corresponding elements will match up. Another common scenario is where a large
|
||||
// union has a union of objects intersected with it. In such cases, if the input was, eg `("a" | "b" | "c") & (string | boolean | {} | {whatever})`,
|
||||
// the result will have the structure `"a" | "b" | "c" | "a" & {} | "b" & {} | "c" & {} | "a" & {whatever} | "b" & {whatever} | "c" & {whatever}`
|
||||
// - the resulting union has a length which is a multiple of the original union, and the elements correspond modulo the length of the original union
|
||||
const related = isRelatedTo(sourceType, (undefinedStrippedTarget as UnionType).types[i % (undefinedStrippedTarget as UnionType).types.length], /*reportErrors*/ false, /*headMessage*/ undefined, intersectionState);
|
||||
if (related) {
|
||||
result &= related;
|
||||
continue;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user