From b44ee8ef4b5f3775c451b7aad90d53e8a284bdd0 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 30 Nov 2018 14:59:43 -0800 Subject: [PATCH 1/6] Added tests for types with overlap across a single property name. --- .../errorsOnUnionsOfOverlappingObjects01.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts b/tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts index aec1821c162..fe1d63cb606 100644 --- a/tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts +++ b/tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts @@ -27,3 +27,21 @@ declare function h(x: Foo | Bar | Other): any; h(x); h({ a: '', b: '' }) + +interface CatDog { cat: any, dog: any } +interface ManBearPig { man: any, bear: any, pig: any } +interface Platypus { platypus: any } + +type ExoticAnimal = + | CatDog + | ManBearPig + | Platypus; + +declare function addToZoo(animal: ExoticAnimal): void; + +addToZoo({ dog: "Barky McBarkface" }); +addToZoo({ man: "Manny", bear: "Coffee" }); + +const manBeer = { man: "Manny", beer: "Coffee" }; +addToZoo({ man: "Manny", beer: "Coffee" }); +addToZoo(manBeer); \ No newline at end of file From 448baaeb2fa7b05014b54b847d376d063759437c Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 30 Nov 2018 15:07:50 -0800 Subject: [PATCH 2/6] Accepted baselines. --- ...sOnUnionsOfOverlappingObjects01.errors.txt | 43 ++++++++++++- .../errorsOnUnionsOfOverlappingObjects01.js | 24 +++++++- ...rorsOnUnionsOfOverlappingObjects01.symbols | 55 +++++++++++++++++ ...errorsOnUnionsOfOverlappingObjects01.types | 61 +++++++++++++++++++ 4 files changed, 180 insertions(+), 3 deletions(-) diff --git a/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.errors.txt b/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.errors.txt index 3944c430f33..353c3e2fe29 100644 --- a/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.errors.txt +++ b/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.errors.txt @@ -8,9 +8,17 @@ tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts(19,3): error TS2345 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(42,10): error TS2345: Argument of type '{ dog: string; }' is not assignable to parameter of type 'ExoticAnimal'. + Property 'platypus' is missing in type '{ dog: string; }' but required in type 'Platypus'. +tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts(43,10): error TS2345: Argument of type '{ man: string; bear: string; }' is not assignable to parameter of type 'ExoticAnimal'. + Property 'pig' is missing in type '{ man: string; bear: string; }' but required in type 'ManBearPig'. +tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts(46,26): error TS2345: Argument of type '{ man: string; beer: string; }' is not assignable to parameter of type 'ExoticAnimal'. + Object literal may only specify known properties, and 'beer' does not exist in type 'ExoticAnimal'. +tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts(47,10): error TS2345: Argument of type '{ man: string; beer: string; }' is not assignable to parameter of type 'ExoticAnimal'. + Type '{ man: string; beer: string; }' is missing the following properties from type 'ManBearPig': bear, pig -==== tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts (3 errors) ==== +==== tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts (7 errors) ==== interface Foo { a: string; b: number; @@ -53,4 +61,35 @@ tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts(24,5): error TS2345 h(x); h({ a: '', b: '' }) - \ No newline at end of file + + interface CatDog { cat: any, dog: any } + interface ManBearPig { man: any, bear: any, pig: any } + interface Platypus { platypus: any } + + type ExoticAnimal = + | CatDog + | ManBearPig + | Platypus; + + declare function addToZoo(animal: ExoticAnimal): void; + + addToZoo({ dog: "Barky McBarkface" }); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '{ dog: string; }' is not assignable to parameter of type 'ExoticAnimal'. +!!! error TS2345: Property 'platypus' is missing in type '{ dog: string; }' but required in type 'Platypus'. +!!! related TS2728 tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts:33:22: 'platypus' is declared here. + addToZoo({ man: "Manny", bear: "Coffee" }); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '{ man: string; bear: string; }' is not assignable to parameter of type 'ExoticAnimal'. +!!! error TS2345: Property 'pig' is missing in type '{ man: string; bear: string; }' but required in type 'ManBearPig'. +!!! related TS2728 tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts:32:45: 'pig' is declared here. + + const manBeer = { man: "Manny", beer: "Coffee" }; + addToZoo({ man: "Manny", beer: "Coffee" }); + ~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '{ man: string; beer: string; }' is not assignable to parameter of type 'ExoticAnimal'. +!!! error TS2345: Object literal may only specify known properties, and 'beer' does not exist in type 'ExoticAnimal'. + addToZoo(manBeer); + ~~~~~~~ +!!! error TS2345: Argument of type '{ man: string; beer: string; }' is not assignable to parameter of type 'ExoticAnimal'. +!!! error TS2345: Type '{ man: string; beer: string; }' is missing the following properties from type 'ManBearPig': bear, pig \ No newline at end of file diff --git a/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.js b/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.js index 36824508b12..a5d63d76f21 100644 --- a/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.js +++ b/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.js @@ -28,7 +28,24 @@ declare function h(x: Foo | Bar | Other): any; h(x); h({ a: '', b: '' }) - + +interface CatDog { cat: any, dog: any } +interface ManBearPig { man: any, bear: any, pig: any } +interface Platypus { platypus: any } + +type ExoticAnimal = + | CatDog + | ManBearPig + | Platypus; + +declare function addToZoo(animal: ExoticAnimal): void; + +addToZoo({ dog: "Barky McBarkface" }); +addToZoo({ man: "Manny", bear: "Coffee" }); + +const manBeer = { man: "Manny", beer: "Coffee" }; +addToZoo({ man: "Manny", beer: "Coffee" }); +addToZoo(manBeer); //// [errorsOnUnionsOfOverlappingObjects01.js] "use strict"; @@ -41,3 +58,8 @@ g(exports.x); g({ a: '', b: '' }); h(exports.x); h({ a: '', b: '' }); +addToZoo({ dog: "Barky McBarkface" }); +addToZoo({ man: "Manny", bear: "Coffee" }); +var manBeer = { man: "Manny", beer: "Coffee" }; +addToZoo({ man: "Manny", beer: "Coffee" }); +addToZoo(manBeer); diff --git a/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.symbols b/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.symbols index 0ce5d6fd2d4..b92f2e895f6 100644 --- a/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.symbols +++ b/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.symbols @@ -75,3 +75,58 @@ h({ a: '', b: '' }) >a : Symbol(a, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 28, 3)) >b : Symbol(b, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 28, 10)) +interface CatDog { cat: any, dog: any } +>CatDog : Symbol(CatDog, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 28, 19)) +>cat : Symbol(CatDog.cat, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 30, 18)) +>dog : Symbol(CatDog.dog, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 30, 28)) + +interface ManBearPig { man: any, bear: any, pig: any } +>ManBearPig : Symbol(ManBearPig, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 30, 39)) +>man : Symbol(ManBearPig.man, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 31, 22)) +>bear : Symbol(ManBearPig.bear, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 31, 32)) +>pig : Symbol(ManBearPig.pig, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 31, 43)) + +interface Platypus { platypus: any } +>Platypus : Symbol(Platypus, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 31, 54)) +>platypus : Symbol(Platypus.platypus, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 32, 20)) + +type ExoticAnimal = +>ExoticAnimal : Symbol(ExoticAnimal, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 32, 36)) + + | CatDog +>CatDog : Symbol(CatDog, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 28, 19)) + + | ManBearPig +>ManBearPig : Symbol(ManBearPig, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 30, 39)) + + | Platypus; +>Platypus : Symbol(Platypus, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 31, 54)) + +declare function addToZoo(animal: ExoticAnimal): void; +>addToZoo : Symbol(addToZoo, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 37, 15)) +>animal : Symbol(animal, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 39, 26)) +>ExoticAnimal : Symbol(ExoticAnimal, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 32, 36)) + +addToZoo({ dog: "Barky McBarkface" }); +>addToZoo : Symbol(addToZoo, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 37, 15)) +>dog : Symbol(dog, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 41, 10)) + +addToZoo({ man: "Manny", bear: "Coffee" }); +>addToZoo : Symbol(addToZoo, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 37, 15)) +>man : Symbol(man, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 42, 10)) +>bear : Symbol(bear, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 42, 24)) + +const manBeer = { man: "Manny", beer: "Coffee" }; +>manBeer : Symbol(manBeer, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 44, 5)) +>man : Symbol(man, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 44, 17)) +>beer : Symbol(beer, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 44, 31)) + +addToZoo({ man: "Manny", beer: "Coffee" }); +>addToZoo : Symbol(addToZoo, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 37, 15)) +>man : Symbol(man, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 45, 10)) +>beer : Symbol(beer, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 45, 24)) + +addToZoo(manBeer); +>addToZoo : Symbol(addToZoo, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 37, 15)) +>manBeer : Symbol(manBeer, Decl(errorsOnUnionsOfOverlappingObjects01.ts, 44, 5)) + diff --git a/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.types b/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.types index a860897d1fa..1e9b1b8c5ef 100644 --- a/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.types +++ b/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.types @@ -80,3 +80,64 @@ h({ a: '', b: '' }) >b : string >'' : "" +interface CatDog { cat: any, dog: any } +>cat : any +>dog : any + +interface ManBearPig { man: any, bear: any, pig: any } +>man : any +>bear : any +>pig : any + +interface Platypus { platypus: any } +>platypus : any + +type ExoticAnimal = +>ExoticAnimal : ExoticAnimal + + | CatDog + | ManBearPig + | Platypus; + +declare function addToZoo(animal: ExoticAnimal): void; +>addToZoo : (animal: ExoticAnimal) => void +>animal : ExoticAnimal + +addToZoo({ dog: "Barky McBarkface" }); +>addToZoo({ dog: "Barky McBarkface" }) : void +>addToZoo : (animal: ExoticAnimal) => void +>{ dog: "Barky McBarkface" } : { dog: string; } +>dog : string +>"Barky McBarkface" : "Barky McBarkface" + +addToZoo({ man: "Manny", bear: "Coffee" }); +>addToZoo({ man: "Manny", bear: "Coffee" }) : void +>addToZoo : (animal: ExoticAnimal) => void +>{ man: "Manny", bear: "Coffee" } : { man: string; bear: string; } +>man : string +>"Manny" : "Manny" +>bear : string +>"Coffee" : "Coffee" + +const manBeer = { man: "Manny", beer: "Coffee" }; +>manBeer : { man: string; beer: string; } +>{ man: "Manny", beer: "Coffee" } : { man: string; beer: string; } +>man : string +>"Manny" : "Manny" +>beer : string +>"Coffee" : "Coffee" + +addToZoo({ man: "Manny", beer: "Coffee" }); +>addToZoo({ man: "Manny", beer: "Coffee" }) : void +>addToZoo : (animal: ExoticAnimal) => void +>{ man: "Manny", beer: "Coffee" } : { man: string; beer: string; } +>man : string +>"Manny" : "Manny" +>beer : string +>"Coffee" : "Coffee" + +addToZoo(manBeer); +>addToZoo(manBeer) : void +>addToZoo : (animal: ExoticAnimal) => void +>manBeer : { man: string; beer: string; } + From ba5ba4187698b36d2e6f96bc23a0be9d3005594f Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 30 Nov 2018 15:09:02 -0800 Subject: [PATCH 3/6] Types are only overlappy if their index types are single 'keyof's or literal types. --- src/compiler/checker.ts | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index b0c22b61341..394f13bc2cc 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -11976,19 +11976,21 @@ namespace ts { bestMatch = target; matchingCount = Infinity; } - else if (overlap.flags & TypeFlags.Union) { - // Some subset overlap if we have only string literals. + else if (isLiteralType(overlap)) { + // 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 = length((overlap as UnionType).types); - if (len >= matchingCount) { + if (overlap.flags & TypeFlags.Union) { + const len = length((overlap as UnionType).types); + if (len >= matchingCount) { + bestMatch = target; + matchingCount = len; + } + } + else if (1 >= matchingCount) { bestMatch = target; - matchingCount = len; + matchingCount = 1; } - } - else if (!(overlap.flags & TypeFlags.Never) && 1 >= matchingCount) { - bestMatch = target; - matchingCount = 1; } } return bestMatch; From ac11aa345eb1e3f21a65c8686c0ca7ce725cb3e3 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 30 Nov 2018 15:16:26 -0800 Subject: [PATCH 4/6] Accepted baselines. --- .../checkJsxChildrenProperty2.errors.txt | 16 ++++++++-------- ...rorsOnUnionsOfOverlappingObjects01.errors.txt | 6 +++--- tests/baselines/reference/inferTypes1.errors.txt | 4 ++-- .../intersectionWithUnionConstraint.errors.txt | 4 ++-- .../reference/iteratorSpreadInArray6.errors.txt | 12 ++---------- .../reference/mappedTypeErrors.errors.txt | 4 ++-- .../reference/mappedTypeErrors2.errors.txt | 4 ++-- ...TypeWithRecursiveSubtypeReduction2.errors.txt | 13 ++++++++++--- 8 files changed, 31 insertions(+), 32 deletions(-) diff --git a/tests/baselines/reference/checkJsxChildrenProperty2.errors.txt b/tests/baselines/reference/checkJsxChildrenProperty2.errors.txt index e2931b3fb5d..0edadf6dc1b 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 not assignable to type 'string'. + Type '(Element | ((name: string) => Element))[]' is missing the following properties from type 'Element': type, props 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 not assignable to type 'string'. + Type '(number | Element)[]' is missing the following properties from type 'Element': type, props 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 not assignable to type 'string'. + Type '(string | Element)[]' is missing the following properties from type 'Element': type, props 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 not assignable to type 'string'. + Type 'Element[]' is missing the following properties from type 'Element': type, props ==== 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 not assignable to type 'string'. +!!! error TS2322: Type '(Element | ((name: string) => Element))[]' is missing the following properties from type 'Element': type, props
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 not assignable to type 'string'. +!!! error TS2322: Type '(number | Element)[]' is missing the following properties from type 'Element': type, props
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 not assignable to type 'string'. +!!! error TS2322: Type '(string | Element)[]' is missing the following properties from type 'Element': type, props
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 not assignable to type 'string'. +!!! error TS2322: Type 'Element[]' is missing the following properties from type 'Element': type, props
My Div
My Div
; \ No newline at end of file diff --git a/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.errors.txt b/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.errors.txt index 353c3e2fe29..1b8dab19b5b 100644 --- a/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.errors.txt +++ b/tests/baselines/reference/errorsOnUnionsOfOverlappingObjects01.errors.txt @@ -9,7 +9,7 @@ tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts(19,3): error TS2345 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(42,10): error TS2345: Argument of type '{ dog: string; }' is not assignable to parameter of type 'ExoticAnimal'. - Property 'platypus' is missing in type '{ dog: string; }' but required in type 'Platypus'. + Property 'cat' is missing in type '{ dog: string; }' but required in type 'CatDog'. tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts(43,10): error TS2345: Argument of type '{ man: string; bear: string; }' is not assignable to parameter of type 'ExoticAnimal'. Property 'pig' is missing in type '{ man: string; bear: string; }' but required in type 'ManBearPig'. tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts(46,26): error TS2345: Argument of type '{ man: string; beer: string; }' is not assignable to parameter of type 'ExoticAnimal'. @@ -76,8 +76,8 @@ tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts(47,10): error TS234 addToZoo({ dog: "Barky McBarkface" }); ~~~~~~~~~~~~~~~~~~~~~~~~~~~ !!! error TS2345: Argument of type '{ dog: string; }' is not assignable to parameter of type 'ExoticAnimal'. -!!! error TS2345: Property 'platypus' is missing in type '{ dog: string; }' but required in type 'Platypus'. -!!! related TS2728 tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts:33:22: 'platypus' is declared here. +!!! error TS2345: Property 'cat' is missing in type '{ dog: string; }' but required in type 'CatDog'. +!!! related TS2728 tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts:31:20: 'cat' is declared here. addToZoo({ man: "Manny", bear: "Coffee" }); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ !!! error TS2345: Argument of type '{ man: string; bear: string; }' is not assignable to parameter of type 'ExoticAnimal'. diff --git a/tests/baselines/reference/inferTypes1.errors.txt b/tests/baselines/reference/inferTypes1.errors.txt index cbe3eab039c..4684b8f5e34 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 'string'. + Type 'T' is not assignable to type 'symbol'. ==== 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 'string'. +!!! error TS2322: Type 'T' is not assignable to type 'symbol'. // Repro from #22302 diff --git a/tests/baselines/reference/intersectionWithUnionConstraint.errors.txt b/tests/baselines/reference/intersectionWithUnionConstraint.errors.txt index 5ed34cbe626..9bdfc6f30f9 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 'string'. + Type 'T & U' is not assignable to type 'number'. 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 'string'. +!!! error TS2322: Type 'T & U' is not assignable to type 'number'. let y2: string | null = x; // Error ~~ !!! error TS2322: Type 'T & U' is not assignable to type 'string | null'. diff --git a/tests/baselines/reference/iteratorSpreadInArray6.errors.txt b/tests/baselines/reference/iteratorSpreadInArray6.errors.txt index 4f547c59ed0..b151757c98f 100644 --- a/tests/baselines/reference/iteratorSpreadInArray6.errors.txt +++ b/tests/baselines/reference/iteratorSpreadInArray6.errors.txt @@ -1,9 +1,5 @@ tests/cases/conformance/es6/spread/iteratorSpreadInArray6.ts(15,14): error TS2345: Argument of type 'symbol[]' is not assignable to parameter of type 'number | ConcatArray'. - Type 'symbol[]' is not assignable to type 'ConcatArray'. - Types of property 'slice' are incompatible. - Type '(start?: number, end?: number) => symbol[]' is not assignable to type '(start?: number, end?: number) => number[]'. - Type 'symbol[]' is not assignable to type 'number[]'. - Type 'symbol' is not assignable to type 'number'. + Type 'symbol[]' is not assignable to type 'number'. ==== tests/cases/conformance/es6/spread/iteratorSpreadInArray6.ts (1 errors) ==== @@ -24,8 +20,4 @@ tests/cases/conformance/es6/spread/iteratorSpreadInArray6.ts(15,14): error TS234 array.concat([...new SymbolIterator]); ~~~~~~~~~~~~~~~~~~~~~~~ !!! error TS2345: Argument of type 'symbol[]' is not assignable to parameter of type 'number | ConcatArray'. -!!! error TS2345: Type 'symbol[]' is not assignable to type 'ConcatArray'. -!!! error TS2345: Types of property 'slice' are incompatible. -!!! error TS2345: Type '(start?: number, end?: number) => symbol[]' is not assignable to type '(start?: number, end?: number) => number[]'. -!!! error TS2345: Type 'symbol[]' is not assignable to type 'number[]'. -!!! error TS2345: Type 'symbol' is not assignable to type 'number'. \ No newline at end of file +!!! error TS2345: Type 'symbol[]' is not assignable to type 'number'. \ No newline at end of file diff --git a/tests/baselines/reference/mappedTypeErrors.errors.txt b/tests/baselines/reference/mappedTypeErrors.errors.txt index 19be0493cc9..254d1cca1a8 100644 --- a/tests/baselines/reference/mappedTypeErrors.errors.txt +++ b/tests/baselines/reference/mappedTypeErrors.errors.txt @@ -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 'string'. + Type 'T' is not assignable to type 'symbol'. 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'. @@ -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 'string'. +!!! error TS2322: Type 'T' is not assignable to type 'symbol'. ~~~~ !!! 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 d48db498154..9178731d189 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 'string'. + Type 'AB[S]' is not assignable to type 'symbol'. 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 'string'. +!!! error TS2322: Type 'AB[S]' is not assignable to type 'symbol'. ~~~~~ !!! error TS2536: Type 'S' cannot be used to index type 'AB'. diff --git a/tests/baselines/reference/unionTypeWithRecursiveSubtypeReduction2.errors.txt b/tests/baselines/reference/unionTypeWithRecursiveSubtypeReduction2.errors.txt index a849878087f..1bd7a693966 100644 --- a/tests/baselines/reference/unionTypeWithRecursiveSubtypeReduction2.errors.txt +++ b/tests/baselines/reference/unionTypeWithRecursiveSubtypeReduction2.errors.txt @@ -5,7 +5,11 @@ tests/cases/compiler/unionTypeWithRecursiveSubtypeReduction2.ts(19,1): error TS2 tests/cases/compiler/unionTypeWithRecursiveSubtypeReduction2.ts(20,1): error TS2322: Type 'Class' is not assignable to type 'Property'. Types of property 'parent' are incompatible. Type 'Namespace' is not assignable to type 'Module | Class'. - Property 'parent' is missing in type 'Namespace' but required in type 'Class'. + Type 'Namespace' is not assignable to type 'Module'. + Types of property 'members' are incompatible. + Type '(Class | Property)[]' is not assignable to type 'Class[]'. + Type 'Class | Property' is not assignable to type 'Class'. + Type 'Property' is not assignable to type 'Class'. ==== tests/cases/compiler/unionTypeWithRecursiveSubtypeReduction2.ts (2 errors) ==== @@ -39,6 +43,9 @@ tests/cases/compiler/unionTypeWithRecursiveSubtypeReduction2.ts(20,1): error TS2 !!! error TS2322: Type 'Class' is not assignable to type 'Property'. !!! error TS2322: Types of property 'parent' are incompatible. !!! error TS2322: Type 'Namespace' is not assignable to type 'Module | Class'. -!!! error TS2322: Property 'parent' is missing in type 'Namespace' but required in type 'Class'. -!!! related TS2728 tests/cases/compiler/unionTypeWithRecursiveSubtypeReduction2.ts:10:12: 'parent' is declared here. +!!! error TS2322: Type 'Namespace' is not assignable to type 'Module'. +!!! error TS2322: Types of property 'members' are incompatible. +!!! error TS2322: Type '(Class | Property)[]' is not assignable to type 'Class[]'. +!!! error TS2322: Type 'Class | Property' is not assignable to type 'Class'. +!!! error TS2322: Type 'Property' is not assignable to type 'Class'. \ No newline at end of file From 6d2c0037fcf4b88c66e8d0f40be3b25c180b7fd3 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 30 Nov 2018 16:42:14 -0800 Subject: [PATCH 5/6] Only count singleton unit types. --- src/compiler/checker.ts | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 8f1faaa368b..da22fecfb8d 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -11976,22 +11976,20 @@ namespace ts { bestMatch = target; matchingCount = Infinity; } - else if (isLiteralType(overlap)) { + else if (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. - if (overlap.flags & TypeFlags.Union) { - const len = length(filter((overlap as UnionType).types, isUnitType)); - if (len >= matchingCount) { - bestMatch = target; - matchingCount = len; - } - } - else if (1 >= matchingCount) { + const len = length(filter((overlap as UnionType).types, isUnitType)); + if (len >= matchingCount) { bestMatch = target; - matchingCount = 1; + matchingCount = len; } } + else if (isUnitType(overlap) && 1 >= matchingCount) { + bestMatch = target; + matchingCount = 1; + } } return bestMatch; } From 8e98943cbd53e38ccac2f80c7c4c8d4a924ab251 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Fri, 30 Nov 2018 16:42:30 -0800 Subject: [PATCH 6/6] Accepted baselines. --- .../checkJsxChildrenProperty2.errors.txt | 16 ++++++++-------- .../reference/iteratorSpreadInArray6.errors.txt | 12 ++++++++++-- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/tests/baselines/reference/checkJsxChildrenProperty2.errors.txt b/tests/baselines/reference/checkJsxChildrenProperty2.errors.txt index 0edadf6dc1b..e2931b3fb5d 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/iteratorSpreadInArray6.errors.txt b/tests/baselines/reference/iteratorSpreadInArray6.errors.txt index b151757c98f..4f547c59ed0 100644 --- a/tests/baselines/reference/iteratorSpreadInArray6.errors.txt +++ b/tests/baselines/reference/iteratorSpreadInArray6.errors.txt @@ -1,5 +1,9 @@ tests/cases/conformance/es6/spread/iteratorSpreadInArray6.ts(15,14): error TS2345: Argument of type 'symbol[]' is not assignable to parameter of type 'number | ConcatArray'. - Type 'symbol[]' is not assignable to type 'number'. + Type 'symbol[]' is not assignable to type 'ConcatArray'. + Types of property 'slice' are incompatible. + Type '(start?: number, end?: number) => symbol[]' is not assignable to type '(start?: number, end?: number) => number[]'. + Type 'symbol[]' is not assignable to type 'number[]'. + Type 'symbol' is not assignable to type 'number'. ==== tests/cases/conformance/es6/spread/iteratorSpreadInArray6.ts (1 errors) ==== @@ -20,4 +24,8 @@ tests/cases/conformance/es6/spread/iteratorSpreadInArray6.ts(15,14): error TS234 array.concat([...new SymbolIterator]); ~~~~~~~~~~~~~~~~~~~~~~~ !!! error TS2345: Argument of type 'symbol[]' is not assignable to parameter of type 'number | ConcatArray'. -!!! error TS2345: Type 'symbol[]' is not assignable to type 'number'. \ No newline at end of file +!!! error TS2345: Type 'symbol[]' is not assignable to type 'ConcatArray'. +!!! error TS2345: Types of property 'slice' are incompatible. +!!! error TS2345: Type '(start?: number, end?: number) => symbol[]' is not assignable to type '(start?: number, end?: number) => number[]'. +!!! error TS2345: Type 'symbol[]' is not assignable to type 'number[]'. +!!! error TS2345: Type 'symbol' is not assignable to type 'number'. \ No newline at end of file