Skip to content

Propagate intersectionState in typeRelatedToSomeType #56207

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

Merged
merged 2 commits into from
Nov 21, 2023
Merged
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
12 changes: 6 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21491,7 +21491,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
eachTypeRelatedToType(source as UnionType, target, reportErrors && !(source.flags & TypeFlags.Primitive), intersectionState);
}
if (target.flags & TypeFlags.Union) {
return typeRelatedToSomeType(getRegularTypeOfObjectLiteral(source), target as UnionType, reportErrors && !(source.flags & TypeFlags.Primitive) && !(target.flags & TypeFlags.Primitive));
return typeRelatedToSomeType(getRegularTypeOfObjectLiteral(source), target as UnionType, reportErrors && !(source.flags & TypeFlags.Primitive) && !(target.flags & TypeFlags.Primitive), intersectionState);
}
if (target.flags & TypeFlags.Intersection) {
return typeRelatedToEachType(source, target as IntersectionType, reportErrors, IntersectionState.Target);
Expand Down Expand Up @@ -21525,7 +21525,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
let result = Ternary.True;
const sourceTypes = source.types;
for (const sourceType of sourceTypes) {
const related = typeRelatedToSomeType(sourceType, target, /*reportErrors*/ false);
const related = typeRelatedToSomeType(sourceType, target, /*reportErrors*/ false, IntersectionState.None);
if (!related) {
return Ternary.False;
}
Expand All @@ -21534,7 +21534,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return result;
}

function typeRelatedToSomeType(source: Type, target: UnionOrIntersectionType, reportErrors: boolean): Ternary {
function typeRelatedToSomeType(source: Type, target: UnionOrIntersectionType, reportErrors: boolean, intersectionState: IntersectionState): Ternary {
const targetTypes = target.types;
if (target.flags & TypeFlags.Union) {
if (containsType(targetTypes, source)) {
Expand All @@ -21561,14 +21561,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
const match = getMatchingUnionConstituentForType(target as UnionType, source);
if (match) {
const related = isRelatedTo(source, match, RecursionFlags.Target, /*reportErrors*/ false);
const related = isRelatedTo(source, match, RecursionFlags.Target, /*reportErrors*/ false, /*headMessage*/ undefined, intersectionState);
if (related) {
return related;
}
}
}
for (const type of targetTypes) {
const related = isRelatedTo(source, type, RecursionFlags.Target, /*reportErrors*/ false);
const related = isRelatedTo(source, type, RecursionFlags.Target, /*reportErrors*/ false, /*headMessage*/ undefined, intersectionState);
if (related) {
return related;
}
Expand All @@ -21577,7 +21577,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// Elaborate only if we can find a best matching type in the target union
const bestMatchingType = getBestMatchingType(source, target, isRelatedTo);
if (bestMatchingType) {
isRelatedTo(source, bestMatchingType, RecursionFlags.Target, /*reportErrors*/ true);
isRelatedTo(source, bestMatchingType, RecursionFlags.Target, /*reportErrors*/ true, /*headMessage*/ undefined, intersectionState);
}
}
return Ternary.False;
Expand Down
25 changes: 25 additions & 0 deletions tests/baselines/reference/nestedExcessPropertyChecking.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,29 @@ nestedExcessPropertyChecking.ts(40,9): error TS2559: Type 'false' has no propert
},
},
};

// Repro from #53412

type BaseItem = {
id: number;
}
type ExtendedItem = BaseItem & {
description: string | null
};

type BaseValue = {
// there are other fields
items: BaseItem[];
}
type ExtendedValue = BaseValue & {
// there are other fields
items: ExtendedItem[];
}

const TEST_VALUE: ExtendedValue = {
items: [
{id: 1, description: null},
{id: 2, description: 'wigglytubble'},
]
};

31 changes: 31 additions & 0 deletions tests/baselines/reference/nestedExcessPropertyChecking.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,31 @@ const response: Query = {
},
},
};

// Repro from #53412

type BaseItem = {
id: number;
}
type ExtendedItem = BaseItem & {
description: string | null
};

type BaseValue = {
// there are other fields
items: BaseItem[];
}
type ExtendedValue = BaseValue & {
// there are other fields
items: ExtendedItem[];
}

const TEST_VALUE: ExtendedValue = {
items: [
{id: 1, description: null},
{id: 2, description: 'wigglytubble'},
]
};


//// [nestedExcessPropertyChecking.js]
Expand All @@ -90,3 +115,9 @@ var response = {
},
},
};
var TEST_VALUE = {
items: [
{ id: 1, description: null },
{ id: 2, description: 'wigglytubble' },
]
};
53 changes: 53 additions & 0 deletions tests/baselines/reference/nestedExcessPropertyChecking.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,56 @@ const response: Query = {
},
};

// Repro from #53412

type BaseItem = {
>BaseItem : Symbol(BaseItem, Decl(nestedExcessPropertyChecking.ts, 62, 2))

id: number;
>id : Symbol(id, Decl(nestedExcessPropertyChecking.ts, 66, 17))
}
type ExtendedItem = BaseItem & {
>ExtendedItem : Symbol(ExtendedItem, Decl(nestedExcessPropertyChecking.ts, 68, 1))
>BaseItem : Symbol(BaseItem, Decl(nestedExcessPropertyChecking.ts, 62, 2))

description: string | null
>description : Symbol(description, Decl(nestedExcessPropertyChecking.ts, 69, 32))

};

type BaseValue = {
>BaseValue : Symbol(BaseValue, Decl(nestedExcessPropertyChecking.ts, 71, 2))

// there are other fields
items: BaseItem[];
>items : Symbol(items, Decl(nestedExcessPropertyChecking.ts, 73, 18))
>BaseItem : Symbol(BaseItem, Decl(nestedExcessPropertyChecking.ts, 62, 2))
}
type ExtendedValue = BaseValue & {
>ExtendedValue : Symbol(ExtendedValue, Decl(nestedExcessPropertyChecking.ts, 76, 1))
>BaseValue : Symbol(BaseValue, Decl(nestedExcessPropertyChecking.ts, 71, 2))

// there are other fields
items: ExtendedItem[];
>items : Symbol(items, Decl(nestedExcessPropertyChecking.ts, 77, 34))
>ExtendedItem : Symbol(ExtendedItem, Decl(nestedExcessPropertyChecking.ts, 68, 1))
}

const TEST_VALUE: ExtendedValue = {
>TEST_VALUE : Symbol(TEST_VALUE, Decl(nestedExcessPropertyChecking.ts, 82, 5))
>ExtendedValue : Symbol(ExtendedValue, Decl(nestedExcessPropertyChecking.ts, 76, 1))

items: [
>items : Symbol(items, Decl(nestedExcessPropertyChecking.ts, 82, 35))

{id: 1, description: null},
>id : Symbol(id, Decl(nestedExcessPropertyChecking.ts, 84, 9))
>description : Symbol(description, Decl(nestedExcessPropertyChecking.ts, 84, 15))

{id: 2, description: 'wigglytubble'},
>id : Symbol(id, Decl(nestedExcessPropertyChecking.ts, 85, 9))
>description : Symbol(description, Decl(nestedExcessPropertyChecking.ts, 85, 15))

]
};

55 changes: 55 additions & 0 deletions tests/baselines/reference/nestedExcessPropertyChecking.types
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,58 @@ const response: Query = {
},
};

// Repro from #53412

type BaseItem = {
>BaseItem : { id: number; }

id: number;
>id : number
}
type ExtendedItem = BaseItem & {
>ExtendedItem : BaseItem & { description: string | null; }

description: string | null
>description : string | null

};

type BaseValue = {
>BaseValue : { items: BaseItem[]; }

// there are other fields
items: BaseItem[];
>items : BaseItem[]
}
type ExtendedValue = BaseValue & {
>ExtendedValue : BaseValue & { items: ExtendedItem[]; }

// there are other fields
items: ExtendedItem[];
>items : ExtendedItem[]
}

const TEST_VALUE: ExtendedValue = {
>TEST_VALUE : ExtendedValue
>{ items: [ {id: 1, description: null}, {id: 2, description: 'wigglytubble'}, ]} : { items: ({ id: number; description: null; } | { id: number; description: string; })[]; }

items: [
>items : ({ id: number; description: null; } | { id: number; description: string; })[]
>[ {id: 1, description: null}, {id: 2, description: 'wigglytubble'}, ] : ({ id: number; description: null; } | { id: number; description: string; })[]

{id: 1, description: null},
>{id: 1, description: null} : { id: number; description: null; }
>id : number
>1 : 1
>description : null

{id: 2, description: 'wigglytubble'},
>{id: 2, description: 'wigglytubble'} : { id: number; description: string; }
>id : number
>2 : 2
>description : string
>'wigglytubble' : "wigglytubble"

]
};

25 changes: 25 additions & 0 deletions tests/cases/compiler/nestedExcessPropertyChecking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,28 @@ const response: Query = {
},
},
};

// Repro from #53412

type BaseItem = {
id: number;
}
type ExtendedItem = BaseItem & {
description: string | null
};

type BaseValue = {
// there are other fields
items: BaseItem[];
}
type ExtendedValue = BaseValue & {
// there are other fields
items: ExtendedItem[];
}

const TEST_VALUE: ExtendedValue = {
items: [
{id: 1, description: null},
{id: 2, description: 'wigglytubble'},
]
};