diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts
index 6ef51bc401ad9..118af6486ed89 100644
--- a/src/compiler/checker.ts
+++ b/src/compiler/checker.ts
@@ -11921,7 +11921,8 @@ namespace ts {
findMatchingDiscriminantType(source, target) ||
findMatchingTypeReferenceOrTypeAliasReference(source, target) ||
findBestTypeForObjectLiteral(source, target) ||
- findBestTypeForInvokable(source, target);
+ findBestTypeForInvokable(source, target) ||
+ findMostOverlappyType(source, target);
isRelatedTo(source, bestMatchingType || targetTypes[targetTypes.length - 1], /*reportErrors*/ true);
}
@@ -11961,6 +11962,34 @@ namespace ts {
}
}
+ function findMostOverlappyType(source: Type, unionTarget: UnionOrIntersectionType) {
+ let bestMatch: Type | undefined;
+ let matchingCount = 0;
+ for (const target of unionTarget.types) {
+ const overlap = getIntersectionType([getIndexType(source), getIndexType(target)]);
+ if (overlap.flags & TypeFlags.Index) {
+ // perfect overlap of keys
+ bestMatch = target;
+ matchingCount = Infinity;
+ }
+ else if (overlap.flags & TypeFlags.Union) {
+ // Some subset overlap if we have only string literals.
+ // If we have a union of index types, it seems likely that we
+ // needed to elaborate between two generic mapped types anyway.
+ const len = length((overlap as UnionType).types);
+ if (len >= matchingCount) {
+ bestMatch = target;
+ matchingCount = len;
+ }
+ }
+ else if (!(overlap.flags & TypeFlags.Never) && 1 >= matchingCount) {
+ bestMatch = target;
+ matchingCount = 1;
+ }
+ }
+ return bestMatch;
+ }
+
// Keep this up-to-date with the same logic within `getApparentTypeOfContextualType`, since they should behave similarly
function findMatchingDiscriminantType(source: Type, target: Type) {
if (target.flags & TypeFlags.Union) {
diff --git a/tests/baselines/reference/checkJsxChildrenProperty2.errors.txt b/tests/baselines/reference/checkJsxChildrenProperty2.errors.txt
index 0edadf6dc1bd9..e2931b3fb5d34 100644
--- a/tests/baselines/reference/checkJsxChildrenProperty2.errors.txt
+++ b/tests/baselines/reference/checkJsxChildrenProperty2.errors.txt
@@ -3,19 +3,19 @@ tests/cases/conformance/jsx/file.tsx(17,11): error TS2710: 'children' are specif
tests/cases/conformance/jsx/file.tsx(31,6): error TS2322: Type '{ children: (Element | ((name: string) => Element))[]; a: number; b: string; }' is not assignable to type 'Prop'.
Types of property 'children' are incompatible.
Type '(Element | ((name: string) => Element))[]' is not assignable to type 'string | Element'.
- Type '(Element | ((name: string) => Element))[]' is missing the following properties from type 'Element': type, props
+ Type '(Element | ((name: string) => Element))[]' is not assignable to type 'string'.
tests/cases/conformance/jsx/file.tsx(37,6): error TS2322: Type '{ children: (number | Element)[]; a: number; b: string; }' is not assignable to type 'Prop'.
Types of property 'children' are incompatible.
Type '(number | Element)[]' is not assignable to type 'string | Element'.
- Type '(number | Element)[]' is missing the following properties from type 'Element': type, props
+ Type '(number | Element)[]' is not assignable to type 'string'.
tests/cases/conformance/jsx/file.tsx(43,6): error TS2322: Type '{ children: (string | Element)[]; a: number; b: string; }' is not assignable to type 'Prop'.
Types of property 'children' are incompatible.
Type '(string | Element)[]' is not assignable to type 'string | Element'.
- Type '(string | Element)[]' is missing the following properties from type 'Element': type, props
+ Type '(string | Element)[]' is not assignable to type 'string'.
tests/cases/conformance/jsx/file.tsx(49,6): error TS2322: Type '{ children: Element[]; a: number; b: string; }' is not assignable to type 'Prop'.
Types of property 'children' are incompatible.
Type 'Element[]' is not assignable to type 'string | Element'.
- Type 'Element[]' is missing the following properties from type 'Element': type, props
+ Type 'Element[]' is not assignable to type 'string'.
==== tests/cases/conformance/jsx/file.tsx (6 errors) ====
@@ -59,7 +59,7 @@ tests/cases/conformance/jsx/file.tsx(49,6): error TS2322: Type '{ children: Elem
!!! error TS2322: Type '{ children: (Element | ((name: string) => Element))[]; a: number; b: string; }' is not assignable to type 'Prop'.
!!! error TS2322: Types of property 'children' are incompatible.
!!! error TS2322: Type '(Element | ((name: string) => Element))[]' is not assignable to type 'string | Element'.
-!!! error TS2322: Type '(Element | ((name: string) => Element))[]' is missing the following properties from type 'Element': type, props
+!!! error TS2322: Type '(Element | ((name: string) => Element))[]' is not assignable to type 'string'.
My Div
{(name: string) => My name {name}
}
;
@@ -70,7 +70,7 @@ tests/cases/conformance/jsx/file.tsx(49,6): error TS2322: Type '{ children: Elem
!!! error TS2322: Type '{ children: (number | Element)[]; a: number; b: string; }' is not assignable to type 'Prop'.
!!! error TS2322: Types of property 'children' are incompatible.
!!! error TS2322: Type '(number | Element)[]' is not assignable to type 'string | Element'.
-!!! error TS2322: Type '(number | Element)[]' is missing the following properties from type 'Element': type, props
+!!! error TS2322: Type '(number | Element)[]' is not assignable to type 'string'.
My Div
{1000000}
;
@@ -81,7 +81,7 @@ tests/cases/conformance/jsx/file.tsx(49,6): error TS2322: Type '{ children: Elem
!!! error TS2322: Type '{ children: (string | Element)[]; a: number; b: string; }' is not assignable to type 'Prop'.
!!! error TS2322: Types of property 'children' are incompatible.
!!! error TS2322: Type '(string | Element)[]' is not assignable to type 'string | Element'.
-!!! error TS2322: Type '(string | Element)[]' is missing the following properties from type 'Element': type, props
+!!! error TS2322: Type '(string | Element)[]' is not assignable to type 'string'.
My Div
hi hi hi!
;
@@ -92,7 +92,7 @@ tests/cases/conformance/jsx/file.tsx(49,6): error TS2322: Type '{ children: Elem
!!! error TS2322: Type '{ children: Element[]; a: number; b: string; }' is not assignable to type 'Prop'.
!!! error TS2322: Types of property 'children' are incompatible.
!!! error TS2322: Type 'Element[]' is not assignable to type 'string | Element'.
-!!! error TS2322: Type 'Element[]' is missing the following properties from type 'Element': type, props
+!!! error TS2322: Type 'Element[]' is not assignable to type 'string'.
My Div
My Div
;
\ No newline at end of file
diff --git a/tests/baselines/reference/contextualTypeWithUnionTypeObjectLiteral.errors.txt b/tests/baselines/reference/contextualTypeWithUnionTypeObjectLiteral.errors.txt
index 99e3adb5f9f6a..733e18307fd43 100644
--- a/tests/baselines/reference/contextualTypeWithUnionTypeObjectLiteral.errors.txt
+++ b/tests/baselines/reference/contextualTypeWithUnionTypeObjectLiteral.errors.txt
@@ -9,12 +9,15 @@ tests/cases/conformance/types/union/contextualTypeWithUnionTypeObjectLiteral.ts(
Type 'string | number' is not assignable to type 'number'.
Type 'string' is not assignable to type 'number'.
tests/cases/conformance/types/union/contextualTypeWithUnionTypeObjectLiteral.ts(21,5): error TS2322: Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: string; anotherP: string; } | { prop: number; }'.
- Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: number; }'.
+ Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: string; anotherP: string; }'.
Types of property 'prop' are incompatible.
- Type 'string | number' is not assignable to type 'number'.
- Type 'string' is not assignable to type 'number'.
+ Type 'string | number' is not assignable to type 'string'.
+ Type 'number' is not assignable to type 'string'.
tests/cases/conformance/types/union/contextualTypeWithUnionTypeObjectLiteral.ts(25,5): error TS2322: Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: string; anotherP: string; } | { prop: number; anotherP1: number; }'.
- Property 'anotherP1' is missing in type '{ prop: string | number; anotherP: string; }' but required in type '{ prop: number; anotherP1: number; }'.
+ Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: string; anotherP: string; }'.
+ Types of property 'prop' are incompatible.
+ Type 'string | number' is not assignable to type 'string'.
+ Type 'number' is not assignable to type 'string'.
tests/cases/conformance/types/union/contextualTypeWithUnionTypeObjectLiteral.ts(29,5): error TS2322: Type '{ prop: string | number; anotherP: string; anotherP1: number; }' is not assignable to type '{ prop: string; anotherP: string; } | { prop: number; anotherP1: number; }'.
Type '{ prop: string | number; anotherP: string; anotherP1: number; }' is not assignable to type '{ prop: number; anotherP1: number; }'.
Types of property 'prop' are incompatible.
@@ -62,18 +65,20 @@ tests/cases/conformance/types/union/contextualTypeWithUnionTypeObjectLiteral.ts(
var objStrOrNum6: { prop: string; anotherP: string; } | { prop: number } = {
~~~~~~~~~~~~
!!! error TS2322: Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: string; anotherP: string; } | { prop: number; }'.
-!!! error TS2322: Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: number; }'.
+!!! error TS2322: Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: string; anotherP: string; }'.
!!! error TS2322: Types of property 'prop' are incompatible.
-!!! error TS2322: Type 'string | number' is not assignable to type 'number'.
-!!! error TS2322: Type 'string' is not assignable to type 'number'.
+!!! error TS2322: Type 'string | number' is not assignable to type 'string'.
+!!! error TS2322: Type 'number' is not assignable to type 'string'.
prop: strOrNumber,
anotherP: str
};
var objStrOrNum7: { prop: string; anotherP: string; } | { prop: number; anotherP1: number } = {
~~~~~~~~~~~~
!!! error TS2322: Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: string; anotherP: string; } | { prop: number; anotherP1: number; }'.
-!!! error TS2322: Property 'anotherP1' is missing in type '{ prop: string | number; anotherP: string; }' but required in type '{ prop: number; anotherP1: number; }'.
-!!! related TS2728 tests/cases/conformance/types/union/contextualTypeWithUnionTypeObjectLiteral.ts:25:73: 'anotherP1' is declared here.
+!!! error TS2322: Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: string; anotherP: string; }'.
+!!! error TS2322: Types of property 'prop' are incompatible.
+!!! error TS2322: Type 'string | number' is not assignable to type 'string'.
+!!! error TS2322: Type 'number' is not assignable to type 'string'.
prop: strOrNumber,
anotherP: str
};
diff --git a/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.errors.txt b/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.errors.txt
new file mode 100644
index 0000000000000..3944c430f33b9
--- /dev/null
+++ b/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.errors.txt
@@ -0,0 +1,56 @@
+tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts(18,3): error TS2345: Argument of type '{ a: string; b: string; }' is not assignable to parameter of type 'Foo | Other'.
+ Type '{ a: string; b: string; }' is not assignable to type 'Foo'.
+ Types of property 'b' are incompatible.
+ Type 'string' is not assignable to type 'number'.
+tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts(19,3): error TS2345: Argument of type '{ a: string; b: string; }' is not assignable to parameter of type 'Foo | Other'.
+ Type '{ a: string; b: string; }' is not assignable to type 'Foo'.
+ Types of property 'b' are incompatible.
+ Type 'string' is not assignable to type 'number'.
+tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts(24,5): error TS2345: Argument of type '{ a: string; b: string; }' is not assignable to parameter of type 'Bar | Other'.
+ Object literal may only specify known properties, and 'a' does not exist in type 'Bar | Other'.
+
+
+==== tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts (3 errors) ====
+ interface Foo {
+ a: string;
+ b: number;
+ };
+
+ interface Bar {
+ b: string;
+ }
+
+ interface Other {
+ totallyUnrelatedProperty: number;
+ }
+
+ export let x = { a: '', b: '' };
+
+ declare function f(x: Foo | Other): any;
+
+ f(x);
+ ~
+!!! error TS2345: Argument of type '{ a: string; b: string; }' is not assignable to parameter of type 'Foo | Other'.
+!!! error TS2345: Type '{ a: string; b: string; }' is not assignable to type 'Foo'.
+!!! error TS2345: Types of property 'b' are incompatible.
+!!! error TS2345: Type 'string' is not assignable to type 'number'.
+ f({ a: '', b: '' })
+ ~~~~~~~~~~~~~~~~
+!!! error TS2345: Argument of type '{ a: string; b: string; }' is not assignable to parameter of type 'Foo | Other'.
+!!! error TS2345: Type '{ a: string; b: string; }' is not assignable to type 'Foo'.
+!!! error TS2345: Types of property 'b' are incompatible.
+!!! error TS2345: Type 'string' is not assignable to type 'number'.
+
+ declare function g(x: Bar | Other): any;
+
+ g(x);
+ g({ a: '', b: '' })
+ ~~~~~
+!!! error TS2345: Argument of type '{ a: string; b: string; }' is not assignable to parameter of type 'Bar | Other'.
+!!! error TS2345: Object literal may only specify known properties, and 'a' does not exist in type 'Bar | Other'.
+
+ declare function h(x: Foo | Bar | Other): any;
+
+ h(x);
+ h({ a: '', b: '' })
+
\ No newline at end of file
diff --git a/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.js b/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.js
new file mode 100644
index 0000000000000..36824508b1259
--- /dev/null
+++ b/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.js
@@ -0,0 +1,43 @@
+//// [errorsOnUnionsOfOverlappingObjects01.ts]
+interface Foo {
+ a: string;
+ b: number;
+};
+
+interface Bar {
+ b: string;
+}
+
+interface Other {
+ totallyUnrelatedProperty: number;
+}
+
+export let x = { a: '', b: '' };
+
+declare function f(x: Foo | Other): any;
+
+f(x);
+f({ a: '', b: '' })
+
+declare function g(x: Bar | Other): any;
+
+g(x);
+g({ a: '', b: '' })
+
+declare function h(x: Foo | Bar | Other): any;
+
+h(x);
+h({ a: '', b: '' })
+
+
+//// [errorsOnUnionsOfOverlappingObjects01.js]
+"use strict";
+exports.__esModule = true;
+;
+exports.x = { a: '', b: '' };
+f(exports.x);
+f({ a: '', b: '' });
+g(exports.x);
+g({ a: '', b: '' });
+h(exports.x);
+h({ a: '', b: '' });
diff --git a/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.symbols b/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.symbols
new file mode 100644
index 0000000000000..0ce5d6fd2d433
--- /dev/null
+++ b/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.symbols
@@ -0,0 +1,77 @@
+=== tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts ===
+interface Foo {
+>Foo : Symbol(Foo, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 0, 0))
+
+ a: string;
+>a : Symbol(Foo.a, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 0, 15))
+
+ b: number;
+>b : Symbol(Foo.b, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 1, 14))
+
+};
+
+interface Bar {
+>Bar : Symbol(Bar, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 3, 2))
+
+ b: string;
+>b : Symbol(Bar.b, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 5, 15))
+}
+
+interface Other {
+>Other : Symbol(Other, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 7, 1))
+
+ totallyUnrelatedProperty: number;
+>totallyUnrelatedProperty : Symbol(Other.totallyUnrelatedProperty, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 9, 17))
+}
+
+export let x = { a: '', b: '' };
+>x : Symbol(x, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 13, 10))
+>a : Symbol(a, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 13, 16))
+>b : Symbol(b, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 13, 23))
+
+declare function f(x: Foo | Other): any;
+>f : Symbol(f, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 13, 32))
+>x : Symbol(x, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 15, 19))
+>Foo : Symbol(Foo, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 0, 0))
+>Other : Symbol(Other, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 7, 1))
+
+f(x);
+>f : Symbol(f, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 13, 32))
+>x : Symbol(x, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 13, 10))
+
+f({ a: '', b: '' })
+>f : Symbol(f, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 13, 32))
+>a : Symbol(a, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 18, 3))
+>b : Symbol(b, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 18, 10))
+
+declare function g(x: Bar | Other): any;
+>g : Symbol(g, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 18, 19))
+>x : Symbol(x, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 20, 19))
+>Bar : Symbol(Bar, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 3, 2))
+>Other : Symbol(Other, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 7, 1))
+
+g(x);
+>g : Symbol(g, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 18, 19))
+>x : Symbol(x, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 13, 10))
+
+g({ a: '', b: '' })
+>g : Symbol(g, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 18, 19))
+>a : Symbol(a, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 23, 3))
+>b : Symbol(b, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 23, 10))
+
+declare function h(x: Foo | Bar | Other): any;
+>h : Symbol(h, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 23, 19))
+>x : Symbol(x, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 25, 19))
+>Foo : Symbol(Foo, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 0, 0))
+>Bar : Symbol(Bar, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 3, 2))
+>Other : Symbol(Other, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 7, 1))
+
+h(x);
+>h : Symbol(h, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 23, 19))
+>x : Symbol(x, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 13, 10))
+
+h({ a: '', b: '' })
+>h : Symbol(h, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 23, 19))
+>a : Symbol(a, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 28, 3))
+>b : Symbol(b, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 28, 10))
+
diff --git a/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.types b/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.types
new file mode 100644
index 0000000000000..a860897d1fa10
--- /dev/null
+++ b/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.types
@@ -0,0 +1,82 @@
+=== tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts ===
+interface Foo {
+ a: string;
+>a : string
+
+ b: number;
+>b : number
+
+};
+
+interface Bar {
+ b: string;
+>b : string
+}
+
+interface Other {
+ totallyUnrelatedProperty: number;
+>totallyUnrelatedProperty : number
+}
+
+export let x = { a: '', b: '' };
+>x : { a: string; b: string; }
+>{ a: '', b: '' } : { a: string; b: string; }
+>a : string
+>'' : ""
+>b : string
+>'' : ""
+
+declare function f(x: Foo | Other): any;
+>f : (x: Foo | Other) => any
+>x : Foo | Other
+
+f(x);
+>f(x) : any
+>f : (x: Foo | Other) => any
+>x : { a: string; b: string; }
+
+f({ a: '', b: '' })
+>f({ a: '', b: '' }) : any
+>f : (x: Foo | Other) => any
+>{ a: '', b: '' } : { a: string; b: string; }
+>a : string
+>'' : ""
+>b : string
+>'' : ""
+
+declare function g(x: Bar | Other): any;
+>g : (x: Bar | Other) => any
+>x : Bar | Other
+
+g(x);
+>g(x) : any
+>g : (x: Bar | Other) => any
+>x : { a: string; b: string; }
+
+g({ a: '', b: '' })
+>g({ a: '', b: '' }) : any
+>g : (x: Bar | Other) => any
+>{ a: '', b: '' } : { a: string; b: string; }
+>a : string
+>'' : ""
+>b : string
+>'' : ""
+
+declare function h(x: Foo | Bar | Other): any;
+>h : (x: Foo | Bar | Other) => any
+>x : Foo | Bar | Other
+
+h(x);
+>h(x) : any
+>h : (x: Foo | Bar | Other) => any
+>x : { a: string; b: string; }
+
+h({ a: '', b: '' })
+>h({ a: '', b: '' }) : any
+>h : (x: Foo | Bar | Other) => any
+>{ a: '', b: '' } : { a: string; b: string; }
+>a : string
+>'' : ""
+>b : string
+>'' : ""
+
diff --git a/tests/baselines/reference/excessPropertyCheckWithUnions.errors.txt b/tests/baselines/reference/excessPropertyCheckWithUnions.errors.txt
index a915d93931dc5..5331add9e8d7a 100644
--- a/tests/baselines/reference/excessPropertyCheckWithUnions.errors.txt
+++ b/tests/baselines/reference/excessPropertyCheckWithUnions.errors.txt
@@ -13,9 +13,9 @@ tests/cases/compiler/excessPropertyCheckWithUnions.ts(39,1): error TS2322: Type
Types of property 'tag' are incompatible.
Type '"A"' is not assignable to type '"C"'.
tests/cases/compiler/excessPropertyCheckWithUnions.ts(40,1): error TS2322: Type '{ tag: "A"; z: true; }' is not assignable to type 'Ambiguous'.
- Type '{ tag: "A"; z: true; }' is not assignable to type '{ tag: "C"; }'.
+ Type '{ tag: "A"; z: true; }' is not assignable to type '{ tag: "B"; z: boolean; }'.
Types of property 'tag' are incompatible.
- Type '"A"' is not assignable to type '"C"'.
+ Type '"A"' is not assignable to type '"B"'.
tests/cases/compiler/excessPropertyCheckWithUnions.ts(49,35): error TS2322: Type '{ a: 1; b: 1; first: string; second: string; }' is not assignable to type 'Overlapping'.
Object literal may only specify known properties, and 'second' does not exist in type '{ a: 1; b: 1; first: string; }'.
tests/cases/compiler/excessPropertyCheckWithUnions.ts(50,35): error TS2322: Type '{ a: 1; b: 1; first: string; third: string; }' is not assignable to type 'Overlapping'.
@@ -91,9 +91,9 @@ tests/cases/compiler/excessPropertyCheckWithUnions.ts(66,9): error TS2322: Type
amb = { tag: "A", z: true }
~~~
!!! error TS2322: Type '{ tag: "A"; z: true; }' is not assignable to type 'Ambiguous'.
-!!! error TS2322: Type '{ tag: "A"; z: true; }' is not assignable to type '{ tag: "C"; }'.
+!!! error TS2322: Type '{ tag: "A"; z: true; }' is not assignable to type '{ tag: "B"; z: boolean; }'.
!!! error TS2322: Types of property 'tag' are incompatible.
-!!! error TS2322: Type '"A"' is not assignable to type '"C"'.
+!!! error TS2322: Type '"A"' is not assignable to type '"B"'.
type Overlapping =
| { a: 1, b: 1, first: string }
diff --git a/tests/baselines/reference/inferTypes1.errors.txt b/tests/baselines/reference/inferTypes1.errors.txt
index 4684b8f5e3437..cbe3eab039c29 100644
--- a/tests/baselines/reference/inferTypes1.errors.txt
+++ b/tests/baselines/reference/inferTypes1.errors.txt
@@ -18,7 +18,7 @@ tests/cases/conformance/types/conditional/inferTypes1.ts(75,43): error TS4081: E
tests/cases/conformance/types/conditional/inferTypes1.ts(82,44): error TS2344: Type 'U' does not satisfy the constraint 'string'.
Type 'number' is not assignable to type 'string'.
tests/cases/conformance/types/conditional/inferTypes1.ts(144,40): error TS2322: Type 'T' is not assignable to type 'string | number | symbol'.
- Type 'T' is not assignable to type 'symbol'.
+ Type 'T' is not assignable to type 'string'.
==== tests/cases/conformance/types/conditional/inferTypes1.ts (16 errors) ====
@@ -202,7 +202,7 @@ tests/cases/conformance/types/conditional/inferTypes1.ts(144,40): error TS2322:
type B = string extends T ? { [P in T]: void; } : T; // Error
~
!!! error TS2322: Type 'T' is not assignable to type 'string | number | symbol'.
-!!! error TS2322: Type 'T' is not assignable to type 'symbol'.
+!!! error TS2322: Type 'T' is not assignable to type 'string'.
// Repro from #22302
diff --git a/tests/baselines/reference/intersectionAndUnionTypes.errors.txt b/tests/baselines/reference/intersectionAndUnionTypes.errors.txt
index 97914fcce1812..4f3b1db01e305 100644
--- a/tests/baselines/reference/intersectionAndUnionTypes.errors.txt
+++ b/tests/baselines/reference/intersectionAndUnionTypes.errors.txt
@@ -4,8 +4,8 @@ tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(20,1): e
Property 'a' is missing in type 'B' but required in type 'A'.
tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(23,1): error TS2322: Type 'A | B' is not assignable to type '(A & B) | (C & D)'.
Type 'A' is not assignable to type '(A & B) | (C & D)'.
- Type 'A' is not assignable to type 'C & D'.
- Property 'c' is missing in type 'A' but required in type 'C'.
+ Type 'A' is not assignable to type 'A & B'.
+ Type 'A' is not assignable to type 'B'.
tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(25,1): error TS2322: Type 'C | D' is not assignable to type '(A & B) | (C & D)'.
Type 'C' is not assignable to type '(A & B) | (C & D)'.
Type 'C' is not assignable to type 'C & D'.
@@ -27,14 +27,14 @@ tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(31,1): e
Type 'A & B' is not assignable to type 'D'.
tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(32,1): error TS2322: Type 'A | B' is not assignable to type '(A & C) | (A & D) | (B & C) | (B & D)'.
Type 'A' is not assignable to type '(A & C) | (A & D) | (B & C) | (B & D)'.
- Type 'A' is not assignable to type 'B & D'.
- Type 'A' is not assignable to type 'B'.
+ Type 'A' is not assignable to type 'A & D'.
+ Property 'd' is missing in type 'A' but required in type 'D'.
tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(33,1): error TS2322: Type 'C & D' is not assignable to type '(A & C) | (A & D) | (B & C) | (B & D)'.
Type 'C & D' is not assignable to type 'B & D'.
Type 'C & D' is not assignable to type 'B'.
tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(34,1): error TS2322: Type 'C | D' is not assignable to type '(A & C) | (A & D) | (B & C) | (B & D)'.
Type 'C' is not assignable to type '(A & C) | (A & D) | (B & C) | (B & D)'.
- Type 'C' is not assignable to type 'B & D'.
+ Type 'C' is not assignable to type 'B & C'.
Property 'b' is missing in type 'C' but required in type 'B'.
tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(35,1): error TS2322: Type '(A & C) | (A & D) | (B & C) | (B & D)' is not assignable to type 'A & B'.
Type 'A & C' is not assignable to type 'A & B'.
@@ -79,9 +79,8 @@ tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(37,1): e
~
!!! error TS2322: Type 'A | B' is not assignable to type '(A & B) | (C & D)'.
!!! error TS2322: Type 'A' is not assignable to type '(A & B) | (C & D)'.
-!!! error TS2322: Type 'A' is not assignable to type 'C & D'.
-!!! error TS2322: Property 'c' is missing in type 'A' but required in type 'C'.
-!!! related TS2728 tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts:3:15: 'c' is declared here.
+!!! error TS2322: Type 'A' is not assignable to type 'A & B'.
+!!! error TS2322: Type 'A' is not assignable to type 'B'.
x = cnd; // Ok
x = cod;
~
@@ -124,8 +123,9 @@ tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(37,1): e
~
!!! error TS2322: Type 'A | B' is not assignable to type '(A & C) | (A & D) | (B & C) | (B & D)'.
!!! error TS2322: Type 'A' is not assignable to type '(A & C) | (A & D) | (B & C) | (B & D)'.
-!!! error TS2322: Type 'A' is not assignable to type 'B & D'.
-!!! error TS2322: Type 'A' is not assignable to type 'B'.
+!!! error TS2322: Type 'A' is not assignable to type 'A & D'.
+!!! error TS2322: Property 'd' is missing in type 'A' but required in type 'D'.
+!!! related TS2728 tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts:4:15: 'd' is declared here.
y = cnd;
~
!!! error TS2322: Type 'C & D' is not assignable to type '(A & C) | (A & D) | (B & C) | (B & D)'.
@@ -135,7 +135,7 @@ tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(37,1): e
~
!!! error TS2322: Type 'C | D' is not assignable to type '(A & C) | (A & D) | (B & C) | (B & D)'.
!!! error TS2322: Type 'C' is not assignable to type '(A & C) | (A & D) | (B & C) | (B & D)'.
-!!! error TS2322: Type 'C' is not assignable to type 'B & D'.
+!!! error TS2322: Type 'C' is not assignable to type 'B & C'.
!!! error TS2322: Property 'b' is missing in type 'C' but required in type 'B'.
!!! related TS2728 tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts:2:15: 'b' is declared here.
anb = y;
diff --git a/tests/baselines/reference/intersectionWithUnionConstraint.errors.txt b/tests/baselines/reference/intersectionWithUnionConstraint.errors.txt
index 9bdfc6f30f9ad..5ed34cbe62699 100644
--- a/tests/baselines/reference/intersectionWithUnionConstraint.errors.txt
+++ b/tests/baselines/reference/intersectionWithUnionConstraint.errors.txt
@@ -1,7 +1,7 @@
tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(7,9): error TS2322: Type 'T & U' is not assignable to type 'string | number'.
Type 'string | undefined' is not assignable to type 'string | number'.
Type 'undefined' is not assignable to type 'string | number'.
- Type 'T & U' is not assignable to type 'number'.
+ Type 'T & U' is not assignable to type 'string'.
tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(8,9): error TS2322: Type 'T & U' is not assignable to type 'string | null'.
Type 'string | undefined' is not assignable to type 'string | null'.
Type 'undefined' is not assignable to type 'string | null'.
@@ -32,7 +32,7 @@ tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(12
!!! error TS2322: Type 'T & U' is not assignable to type 'string | number'.
!!! error TS2322: Type 'string | undefined' is not assignable to type 'string | number'.
!!! error TS2322: Type 'undefined' is not assignable to type 'string | number'.
-!!! error TS2322: Type 'T & U' is not assignable to type 'number'.
+!!! error TS2322: Type 'T & U' is not assignable to type 'string'.
let y2: string | null = x; // Error
~~
!!! error TS2322: Type 'T & U' is not assignable to type 'string | null'.
diff --git a/tests/baselines/reference/keyofAndIndexedAccess.types b/tests/baselines/reference/keyofAndIndexedAccess.types
index 9a14549db4fd5..f4c47b26df0c4 100644
--- a/tests/baselines/reference/keyofAndIndexedAccess.types
+++ b/tests/baselines/reference/keyofAndIndexedAccess.types
@@ -58,7 +58,7 @@ type K00 = keyof any; // string
>K00 : string | number | symbol
type K01 = keyof string; // "toString" | "charAt" | ...
->K01 : number | "length" | "toString" | "concat" | "slice" | "indexOf" | "lastIndexOf" | "charAt" | "charCodeAt" | "localeCompare" | "match" | "replace" | "search" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "substr" | "valueOf"
+>K01 : number | "length" | "toString" | "concat" | "slice" | "indexOf" | "lastIndexOf" | "valueOf" | "charAt" | "charCodeAt" | "localeCompare" | "match" | "replace" | "search" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "substr"
type K02 = keyof number; // "toString" | "toFixed" | "toExponential" | ...
>K02 : "toString" | "toLocaleString" | "valueOf" | "toFixed" | "toExponential" | "toPrecision"
diff --git a/tests/baselines/reference/keyofAndIndexedAccessErrors.types b/tests/baselines/reference/keyofAndIndexedAccessErrors.types
index cbf56e6f67107..68dc361c99ee3 100644
--- a/tests/baselines/reference/keyofAndIndexedAccessErrors.types
+++ b/tests/baselines/reference/keyofAndIndexedAccessErrors.types
@@ -23,7 +23,7 @@ type T00 = keyof K0; // Error
>T00 : string | number | symbol
type T01 = keyof Object;
->T01 : "toString" | "constructor" | "toLocaleString" | "valueOf" | "hasOwnProperty" | "isPrototypeOf" | "propertyIsEnumerable"
+>T01 : "toString" | "valueOf" | "toLocaleString" | "constructor" | "hasOwnProperty" | "isPrototypeOf" | "propertyIsEnumerable"
type T02 = keyof keyof Object;
>T02 : number | "length" | "toString" | "valueOf" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "substr"
diff --git a/tests/baselines/reference/mappedTypeErrors.errors.txt b/tests/baselines/reference/mappedTypeErrors.errors.txt
index 0ae4ebc468121..19be0493cc9d6 100644
--- a/tests/baselines/reference/mappedTypeErrors.errors.txt
+++ b/tests/baselines/reference/mappedTypeErrors.errors.txt
@@ -1,8 +1,8 @@
tests/cases/conformance/types/mapped/mappedTypeErrors.ts(19,20): error TS2313: Type parameter 'P' has a circular constraint.
tests/cases/conformance/types/mapped/mappedTypeErrors.ts(21,20): error TS2322: Type 'Date' is not assignable to type 'string | number | symbol'.
- Type 'Date' is not assignable to type 'symbol'.
+ Type 'Date' is not assignable to type 'number'.
tests/cases/conformance/types/mapped/mappedTypeErrors.ts(22,19): error TS2344: Type 'Date' does not satisfy the constraint 'string | number | symbol'.
- Type 'Date' is not assignable to type 'symbol'.
+ Type 'Date' is not assignable to type 'number'.
tests/cases/conformance/types/mapped/mappedTypeErrors.ts(25,24): error TS2344: Type '"foo"' does not satisfy the constraint '"name" | "width" | "height" | "visible"'.
tests/cases/conformance/types/mapped/mappedTypeErrors.ts(26,24): error TS2344: Type '"name" | "foo"' does not satisfy the constraint '"name" | "width" | "height" | "visible"'.
Type '"foo"' is not assignable to type '"name" | "width" | "height" | "visible"'.
@@ -37,7 +37,7 @@ tests/cases/conformance/types/mapped/mappedTypeErrors.ts(128,16): error TS2322:
tests/cases/conformance/types/mapped/mappedTypeErrors.ts(129,25): error TS2322: Type 'string' is not assignable to type 'number | undefined'.
tests/cases/conformance/types/mapped/mappedTypeErrors.ts(130,39): error TS2322: Type 'string' is not assignable to type 'number | undefined'.
tests/cases/conformance/types/mapped/mappedTypeErrors.ts(136,16): error TS2322: Type 'T' is not assignable to type 'string | number | symbol'.
- Type 'T' is not assignable to type 'symbol'.
+ Type 'T' is not assignable to type 'string'.
tests/cases/conformance/types/mapped/mappedTypeErrors.ts(136,21): error TS2536: Type 'P' cannot be used to index type 'T'.
tests/cases/conformance/types/mapped/mappedTypeErrors.ts(148,17): error TS2339: Property 'foo' does not exist on type 'Pick'.
tests/cases/conformance/types/mapped/mappedTypeErrors.ts(152,17): error TS2339: Property 'foo' does not exist on type 'Record'.
@@ -69,11 +69,11 @@ tests/cases/conformance/types/mapped/mappedTypeErrors.ts(152,17): error TS2339:
type T02 = { [P in Date]: number }; // Error
~~~~
!!! error TS2322: Type 'Date' is not assignable to type 'string | number | symbol'.
-!!! error TS2322: Type 'Date' is not assignable to type 'symbol'.
+!!! error TS2322: Type 'Date' is not assignable to type 'number'.
type T03 = Record; // Error
~~~~
!!! error TS2344: Type 'Date' does not satisfy the constraint 'string | number | symbol'.
-!!! error TS2344: Type 'Date' is not assignable to type 'symbol'.
+!!! error TS2344: Type 'Date' is not assignable to type 'number'.
type T10 = Pick;
type T11 = Pick; // Error
@@ -249,7 +249,7 @@ tests/cases/conformance/types/mapped/mappedTypeErrors.ts(152,17): error TS2339:
pt: {[P in T]?: T[P]}, // note: should be in keyof T
~
!!! error TS2322: Type 'T' is not assignable to type 'string | number | symbol'.
-!!! error TS2322: Type 'T' is not assignable to type 'symbol'.
+!!! error TS2322: Type 'T' is not assignable to type 'string'.
~~~~
!!! error TS2536: Type 'P' cannot be used to index type 'T'.
};
diff --git a/tests/baselines/reference/mappedTypeErrors2.errors.txt b/tests/baselines/reference/mappedTypeErrors2.errors.txt
index 9178731d1894d..d48db498154cb 100644
--- a/tests/baselines/reference/mappedTypeErrors2.errors.txt
+++ b/tests/baselines/reference/mappedTypeErrors2.errors.txt
@@ -2,7 +2,7 @@ tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(9,30): error TS2536: T
tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(13,30): error TS2536: Type 'K' cannot be used to index type 'T3'.
tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(15,38): error TS2536: Type 'S' cannot be used to index type '{ [key in AB[S]]: true; }'.
tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(15,47): error TS2322: Type 'AB[S]' is not assignable to type 'string | number | symbol'.
- Type 'AB[S]' is not assignable to type 'symbol'.
+ Type 'AB[S]' is not assignable to type 'string'.
tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(15,47): error TS2536: Type 'S' cannot be used to index type 'AB'.
tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(17,49): error TS2536: Type 'L' cannot be used to index type '{ [key in AB[S]]: true; }'.
@@ -31,7 +31,7 @@ tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(17,49): error TS2536:
!!! error TS2536: Type 'S' cannot be used to index type '{ [key in AB[S]]: true; }'.
~~~~~
!!! error TS2322: Type 'AB[S]' is not assignable to type 'string | number | symbol'.
-!!! error TS2322: Type 'AB[S]' is not assignable to type 'symbol'.
+!!! error TS2322: Type 'AB[S]' is not assignable to type 'string'.
~~~~~
!!! error TS2536: Type 'S' cannot be used to index type 'AB'.
diff --git a/tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts b/tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts
new file mode 100644
index 0000000000000..aec1821c1624a
--- /dev/null
+++ b/tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts
@@ -0,0 +1,29 @@
+interface Foo {
+ a: string;
+ b: number;
+};
+
+interface Bar {
+ b: string;
+}
+
+interface Other {
+ totallyUnrelatedProperty: number;
+}
+
+export let x = { a: '', b: '' };
+
+declare function f(x: Foo | Other): any;
+
+f(x);
+f({ a: '', b: '' })
+
+declare function g(x: Bar | Other): any;
+
+g(x);
+g({ a: '', b: '' })
+
+declare function h(x: Foo | Bar | Other): any;
+
+h(x);
+h({ a: '', b: '' })