From 639d9ebb15cd3cc94d76a5fe78c96ac95affe8bf Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Wed, 20 Jun 2018 22:21:53 -0700 Subject: [PATCH 1/6] Added test. --- .../unionTypeErrorMessageTypeRefs01.ts | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 tests/cases/compiler/unionTypeErrorMessageTypeRefs01.ts diff --git a/tests/cases/compiler/unionTypeErrorMessageTypeRefs01.ts b/tests/cases/compiler/unionTypeErrorMessageTypeRefs01.ts new file mode 100644 index 00000000000..5dfd27ded88 --- /dev/null +++ b/tests/cases/compiler/unionTypeErrorMessageTypeRefs01.ts @@ -0,0 +1,51 @@ + +interface Foo { foo: any } +interface Bar { bar: any } +interface Baz { baz: any } +interface Kwah { kwah: any } + +//////// + +interface A { + aProp: T; +} + +interface B { + bProp: T; +} + +interface C { + cProp: T; +} + +declare const a: A; +declare const b: B; +declare const c: C; +declare let thingOfInterfaces: A | B | C; + +thingOfInterfaces = a; +thingOfInterfaces = b; +thingOfInterfaces = c; + +//////// + +type X = { + xProp: T; +} + +type Y = { + yProp: T; +} + +type Z = { + zProp: T; +} + +declare const x: X; +declare const y: Y; +declare const z: Z; +declare let thingOfTypeAliases: X | Y | Z; + +thingOfTypeAliases = x; +thingOfTypeAliases = y; +thingOfTypeAliases = z; \ No newline at end of file From 9ba2eff4b0010e30b8ca3575f1e00ae4b4ee0caa Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Wed, 20 Jun 2018 22:22:30 -0700 Subject: [PATCH 2/6] Accepted baselines. --- ...unionTypeErrorMessageTypeRefs01.errors.txt | 99 ++++++++++++ .../unionTypeErrorMessageTypeRefs01.js | 59 +++++++ .../unionTypeErrorMessageTypeRefs01.symbols | 147 +++++++++++++++++ .../unionTypeErrorMessageTypeRefs01.types | 153 ++++++++++++++++++ 4 files changed, 458 insertions(+) create mode 100644 tests/baselines/reference/unionTypeErrorMessageTypeRefs01.errors.txt create mode 100644 tests/baselines/reference/unionTypeErrorMessageTypeRefs01.js create mode 100644 tests/baselines/reference/unionTypeErrorMessageTypeRefs01.symbols create mode 100644 tests/baselines/reference/unionTypeErrorMessageTypeRefs01.types diff --git a/tests/baselines/reference/unionTypeErrorMessageTypeRefs01.errors.txt b/tests/baselines/reference/unionTypeErrorMessageTypeRefs01.errors.txt new file mode 100644 index 00000000000..63683c87610 --- /dev/null +++ b/tests/baselines/reference/unionTypeErrorMessageTypeRefs01.errors.txt @@ -0,0 +1,99 @@ +tests/cases/compiler/unionTypeErrorMessageTypeRefs01.ts(25,1): error TS2322: Type 'A' is not assignable to type 'A | B | C'. + Type 'A' is not assignable to type 'C'. + Property 'cProp' is missing in type 'A'. +tests/cases/compiler/unionTypeErrorMessageTypeRefs01.ts(26,1): error TS2322: Type 'B' is not assignable to type 'A | B | C'. + Type 'B' is not assignable to type 'C'. + Property 'cProp' is missing in type 'B'. +tests/cases/compiler/unionTypeErrorMessageTypeRefs01.ts(27,1): error TS2322: Type 'C' is not assignable to type 'A | B | C'. + Type 'C' is not assignable to type 'C'. + Type 'Foo' is not assignable to type 'Kwah'. + Property 'kwah' is missing in type 'Foo'. +tests/cases/compiler/unionTypeErrorMessageTypeRefs01.ts(48,1): error TS2322: Type 'X' is not assignable to type 'X | Y | Z'. + Type 'X' is not assignable to type 'Z'. + Property 'zProp' is missing in type 'X'. +tests/cases/compiler/unionTypeErrorMessageTypeRefs01.ts(49,1): error TS2322: Type 'Y' is not assignable to type 'X | Y | Z'. + Type 'Y' is not assignable to type 'Z'. + Property 'zProp' is missing in type 'Y'. +tests/cases/compiler/unionTypeErrorMessageTypeRefs01.ts(50,1): error TS2322: Type 'Z' is not assignable to type 'X | Y | Z'. + Type 'Z' is not assignable to type 'Z'. + Types of property 'zProp' are incompatible. + Type 'Foo' is not assignable to type 'Kwah'. + + +==== tests/cases/compiler/unionTypeErrorMessageTypeRefs01.ts (6 errors) ==== + interface Foo { foo: any } + interface Bar { bar: any } + interface Baz { baz: any } + interface Kwah { kwah: any } + + //////// + + interface A { + aProp: T; + } + + interface B { + bProp: T; + } + + interface C { + cProp: T; + } + + declare const a: A; + declare const b: B; + declare const c: C; + declare let thingOfInterfaces: A | B | C; + + thingOfInterfaces = a; + ~~~~~~~~~~~~~~~~~ +!!! error TS2322: Type 'A' is not assignable to type 'A | B | C'. +!!! error TS2322: Type 'A' is not assignable to type 'C'. +!!! error TS2322: Property 'cProp' is missing in type 'A'. + thingOfInterfaces = b; + ~~~~~~~~~~~~~~~~~ +!!! error TS2322: Type 'B' is not assignable to type 'A | B | C'. +!!! error TS2322: Type 'B' is not assignable to type 'C'. +!!! error TS2322: Property 'cProp' is missing in type 'B'. + thingOfInterfaces = c; + ~~~~~~~~~~~~~~~~~ +!!! error TS2322: Type 'C' is not assignable to type 'A | B | C'. +!!! error TS2322: Type 'C' is not assignable to type 'C'. +!!! error TS2322: Type 'Foo' is not assignable to type 'Kwah'. +!!! error TS2322: Property 'kwah' is missing in type 'Foo'. + + //////// + + type X = { + xProp: T; + } + + type Y = { + yProp: T; + } + + type Z = { + zProp: T; + } + + declare const x: X; + declare const y: Y; + declare const z: Z; + declare let thingOfTypeAliases: X | Y | Z; + + thingOfTypeAliases = x; + ~~~~~~~~~~~~~~~~~~ +!!! error TS2322: Type 'X' is not assignable to type 'X | Y | Z'. +!!! error TS2322: Type 'X' is not assignable to type 'Z'. +!!! error TS2322: Property 'zProp' is missing in type 'X'. + thingOfTypeAliases = y; + ~~~~~~~~~~~~~~~~~~ +!!! error TS2322: Type 'Y' is not assignable to type 'X | Y | Z'. +!!! error TS2322: Type 'Y' is not assignable to type 'Z'. +!!! error TS2322: Property 'zProp' is missing in type 'Y'. + thingOfTypeAliases = z; + ~~~~~~~~~~~~~~~~~~ +!!! error TS2322: Type 'Z' is not assignable to type 'X | Y | Z'. +!!! error TS2322: Type 'Z' is not assignable to type 'Z'. +!!! error TS2322: Types of property 'zProp' are incompatible. +!!! error TS2322: Type 'Foo' is not assignable to type 'Kwah'. \ No newline at end of file diff --git a/tests/baselines/reference/unionTypeErrorMessageTypeRefs01.js b/tests/baselines/reference/unionTypeErrorMessageTypeRefs01.js new file mode 100644 index 00000000000..defd59035a5 --- /dev/null +++ b/tests/baselines/reference/unionTypeErrorMessageTypeRefs01.js @@ -0,0 +1,59 @@ +//// [unionTypeErrorMessageTypeRefs01.ts] +interface Foo { foo: any } +interface Bar { bar: any } +interface Baz { baz: any } +interface Kwah { kwah: any } + +//////// + +interface A { + aProp: T; +} + +interface B { + bProp: T; +} + +interface C { + cProp: T; +} + +declare const a: A; +declare const b: B; +declare const c: C; +declare let thingOfInterfaces: A | B | C; + +thingOfInterfaces = a; +thingOfInterfaces = b; +thingOfInterfaces = c; + +//////// + +type X = { + xProp: T; +} + +type Y = { + yProp: T; +} + +type Z = { + zProp: T; +} + +declare const x: X; +declare const y: Y; +declare const z: Z; +declare let thingOfTypeAliases: X | Y | Z; + +thingOfTypeAliases = x; +thingOfTypeAliases = y; +thingOfTypeAliases = z; + +//// [unionTypeErrorMessageTypeRefs01.js] +thingOfInterfaces = a; +thingOfInterfaces = b; +thingOfInterfaces = c; +thingOfTypeAliases = x; +thingOfTypeAliases = y; +thingOfTypeAliases = z; diff --git a/tests/baselines/reference/unionTypeErrorMessageTypeRefs01.symbols b/tests/baselines/reference/unionTypeErrorMessageTypeRefs01.symbols new file mode 100644 index 00000000000..1788d27a7dc --- /dev/null +++ b/tests/baselines/reference/unionTypeErrorMessageTypeRefs01.symbols @@ -0,0 +1,147 @@ +=== tests/cases/compiler/unionTypeErrorMessageTypeRefs01.ts === +interface Foo { foo: any } +>Foo : Symbol(Foo, Decl(unionTypeErrorMessageTypeRefs01.ts, 0, 0)) +>foo : Symbol(Foo.foo, Decl(unionTypeErrorMessageTypeRefs01.ts, 0, 15)) + +interface Bar { bar: any } +>Bar : Symbol(Bar, Decl(unionTypeErrorMessageTypeRefs01.ts, 0, 26)) +>bar : Symbol(Bar.bar, Decl(unionTypeErrorMessageTypeRefs01.ts, 1, 15)) + +interface Baz { baz: any } +>Baz : Symbol(Baz, Decl(unionTypeErrorMessageTypeRefs01.ts, 1, 26)) +>baz : Symbol(Baz.baz, Decl(unionTypeErrorMessageTypeRefs01.ts, 2, 15)) + +interface Kwah { kwah: any } +>Kwah : Symbol(Kwah, Decl(unionTypeErrorMessageTypeRefs01.ts, 2, 26)) +>kwah : Symbol(Kwah.kwah, Decl(unionTypeErrorMessageTypeRefs01.ts, 3, 16)) + +//////// + +interface A { +>A : Symbol(A, Decl(unionTypeErrorMessageTypeRefs01.ts, 3, 28)) +>T : Symbol(T, Decl(unionTypeErrorMessageTypeRefs01.ts, 7, 12)) + + aProp: T; +>aProp : Symbol(A.aProp, Decl(unionTypeErrorMessageTypeRefs01.ts, 7, 16)) +>T : Symbol(T, Decl(unionTypeErrorMessageTypeRefs01.ts, 7, 12)) +} + +interface B { +>B : Symbol(B, Decl(unionTypeErrorMessageTypeRefs01.ts, 9, 1)) +>T : Symbol(T, Decl(unionTypeErrorMessageTypeRefs01.ts, 11, 12)) + + bProp: T; +>bProp : Symbol(B.bProp, Decl(unionTypeErrorMessageTypeRefs01.ts, 11, 16)) +>T : Symbol(T, Decl(unionTypeErrorMessageTypeRefs01.ts, 11, 12)) +} + +interface C { +>C : Symbol(C, Decl(unionTypeErrorMessageTypeRefs01.ts, 13, 1)) +>T : Symbol(T, Decl(unionTypeErrorMessageTypeRefs01.ts, 15, 12)) + + cProp: T; +>cProp : Symbol(C.cProp, Decl(unionTypeErrorMessageTypeRefs01.ts, 15, 16)) +>T : Symbol(T, Decl(unionTypeErrorMessageTypeRefs01.ts, 15, 12)) +} + +declare const a: A; +>a : Symbol(a, Decl(unionTypeErrorMessageTypeRefs01.ts, 19, 13)) +>A : Symbol(A, Decl(unionTypeErrorMessageTypeRefs01.ts, 3, 28)) +>Foo : Symbol(Foo, Decl(unionTypeErrorMessageTypeRefs01.ts, 0, 0)) + +declare const b: B; +>b : Symbol(b, Decl(unionTypeErrorMessageTypeRefs01.ts, 20, 13)) +>B : Symbol(B, Decl(unionTypeErrorMessageTypeRefs01.ts, 9, 1)) +>Foo : Symbol(Foo, Decl(unionTypeErrorMessageTypeRefs01.ts, 0, 0)) + +declare const c: C; +>c : Symbol(c, Decl(unionTypeErrorMessageTypeRefs01.ts, 21, 13)) +>C : Symbol(C, Decl(unionTypeErrorMessageTypeRefs01.ts, 13, 1)) +>Foo : Symbol(Foo, Decl(unionTypeErrorMessageTypeRefs01.ts, 0, 0)) + +declare let thingOfInterfaces: A | B | C; +>thingOfInterfaces : Symbol(thingOfInterfaces, Decl(unionTypeErrorMessageTypeRefs01.ts, 22, 11)) +>A : Symbol(A, Decl(unionTypeErrorMessageTypeRefs01.ts, 3, 28)) +>Bar : Symbol(Bar, Decl(unionTypeErrorMessageTypeRefs01.ts, 0, 26)) +>B : Symbol(B, Decl(unionTypeErrorMessageTypeRefs01.ts, 9, 1)) +>Baz : Symbol(Baz, Decl(unionTypeErrorMessageTypeRefs01.ts, 1, 26)) +>C : Symbol(C, Decl(unionTypeErrorMessageTypeRefs01.ts, 13, 1)) +>Kwah : Symbol(Kwah, Decl(unionTypeErrorMessageTypeRefs01.ts, 2, 26)) + +thingOfInterfaces = a; +>thingOfInterfaces : Symbol(thingOfInterfaces, Decl(unionTypeErrorMessageTypeRefs01.ts, 22, 11)) +>a : Symbol(a, Decl(unionTypeErrorMessageTypeRefs01.ts, 19, 13)) + +thingOfInterfaces = b; +>thingOfInterfaces : Symbol(thingOfInterfaces, Decl(unionTypeErrorMessageTypeRefs01.ts, 22, 11)) +>b : Symbol(b, Decl(unionTypeErrorMessageTypeRefs01.ts, 20, 13)) + +thingOfInterfaces = c; +>thingOfInterfaces : Symbol(thingOfInterfaces, Decl(unionTypeErrorMessageTypeRefs01.ts, 22, 11)) +>c : Symbol(c, Decl(unionTypeErrorMessageTypeRefs01.ts, 21, 13)) + +//////// + +type X = { +>X : Symbol(X, Decl(unionTypeErrorMessageTypeRefs01.ts, 26, 22)) +>T : Symbol(T, Decl(unionTypeErrorMessageTypeRefs01.ts, 30, 7)) + + xProp: T; +>xProp : Symbol(xProp, Decl(unionTypeErrorMessageTypeRefs01.ts, 30, 13)) +>T : Symbol(T, Decl(unionTypeErrorMessageTypeRefs01.ts, 30, 7)) +} + +type Y = { +>Y : Symbol(Y, Decl(unionTypeErrorMessageTypeRefs01.ts, 32, 1)) +>T : Symbol(T, Decl(unionTypeErrorMessageTypeRefs01.ts, 34, 7)) + + yProp: T; +>yProp : Symbol(yProp, Decl(unionTypeErrorMessageTypeRefs01.ts, 34, 13)) +>T : Symbol(T, Decl(unionTypeErrorMessageTypeRefs01.ts, 34, 7)) +} + +type Z = { +>Z : Symbol(Z, Decl(unionTypeErrorMessageTypeRefs01.ts, 36, 1)) +>T : Symbol(T, Decl(unionTypeErrorMessageTypeRefs01.ts, 38, 7)) + + zProp: T; +>zProp : Symbol(zProp, Decl(unionTypeErrorMessageTypeRefs01.ts, 38, 13)) +>T : Symbol(T, Decl(unionTypeErrorMessageTypeRefs01.ts, 38, 7)) +} + +declare const x: X; +>x : Symbol(x, Decl(unionTypeErrorMessageTypeRefs01.ts, 42, 13)) +>X : Symbol(X, Decl(unionTypeErrorMessageTypeRefs01.ts, 26, 22)) +>Foo : Symbol(Foo, Decl(unionTypeErrorMessageTypeRefs01.ts, 0, 0)) + +declare const y: Y; +>y : Symbol(y, Decl(unionTypeErrorMessageTypeRefs01.ts, 43, 13)) +>Y : Symbol(Y, Decl(unionTypeErrorMessageTypeRefs01.ts, 32, 1)) +>Foo : Symbol(Foo, Decl(unionTypeErrorMessageTypeRefs01.ts, 0, 0)) + +declare const z: Z; +>z : Symbol(z, Decl(unionTypeErrorMessageTypeRefs01.ts, 44, 13)) +>Z : Symbol(Z, Decl(unionTypeErrorMessageTypeRefs01.ts, 36, 1)) +>Foo : Symbol(Foo, Decl(unionTypeErrorMessageTypeRefs01.ts, 0, 0)) + +declare let thingOfTypeAliases: X | Y | Z; +>thingOfTypeAliases : Symbol(thingOfTypeAliases, Decl(unionTypeErrorMessageTypeRefs01.ts, 45, 11)) +>X : Symbol(X, Decl(unionTypeErrorMessageTypeRefs01.ts, 26, 22)) +>Bar : Symbol(Bar, Decl(unionTypeErrorMessageTypeRefs01.ts, 0, 26)) +>Y : Symbol(Y, Decl(unionTypeErrorMessageTypeRefs01.ts, 32, 1)) +>Baz : Symbol(Baz, Decl(unionTypeErrorMessageTypeRefs01.ts, 1, 26)) +>Z : Symbol(Z, Decl(unionTypeErrorMessageTypeRefs01.ts, 36, 1)) +>Kwah : Symbol(Kwah, Decl(unionTypeErrorMessageTypeRefs01.ts, 2, 26)) + +thingOfTypeAliases = x; +>thingOfTypeAliases : Symbol(thingOfTypeAliases, Decl(unionTypeErrorMessageTypeRefs01.ts, 45, 11)) +>x : Symbol(x, Decl(unionTypeErrorMessageTypeRefs01.ts, 42, 13)) + +thingOfTypeAliases = y; +>thingOfTypeAliases : Symbol(thingOfTypeAliases, Decl(unionTypeErrorMessageTypeRefs01.ts, 45, 11)) +>y : Symbol(y, Decl(unionTypeErrorMessageTypeRefs01.ts, 43, 13)) + +thingOfTypeAliases = z; +>thingOfTypeAliases : Symbol(thingOfTypeAliases, Decl(unionTypeErrorMessageTypeRefs01.ts, 45, 11)) +>z : Symbol(z, Decl(unionTypeErrorMessageTypeRefs01.ts, 44, 13)) + diff --git a/tests/baselines/reference/unionTypeErrorMessageTypeRefs01.types b/tests/baselines/reference/unionTypeErrorMessageTypeRefs01.types new file mode 100644 index 00000000000..7010519b559 --- /dev/null +++ b/tests/baselines/reference/unionTypeErrorMessageTypeRefs01.types @@ -0,0 +1,153 @@ +=== tests/cases/compiler/unionTypeErrorMessageTypeRefs01.ts === +interface Foo { foo: any } +>Foo : Foo +>foo : any + +interface Bar { bar: any } +>Bar : Bar +>bar : any + +interface Baz { baz: any } +>Baz : Baz +>baz : any + +interface Kwah { kwah: any } +>Kwah : Kwah +>kwah : any + +//////// + +interface A { +>A : A +>T : T + + aProp: T; +>aProp : T +>T : T +} + +interface B { +>B : B +>T : T + + bProp: T; +>bProp : T +>T : T +} + +interface C { +>C : C +>T : T + + cProp: T; +>cProp : T +>T : T +} + +declare const a: A; +>a : A +>A : A +>Foo : Foo + +declare const b: B; +>b : B +>B : B +>Foo : Foo + +declare const c: C; +>c : C +>C : C +>Foo : Foo + +declare let thingOfInterfaces: A | B | C; +>thingOfInterfaces : A | B | C +>A : A +>Bar : Bar +>B : B +>Baz : Baz +>C : C +>Kwah : Kwah + +thingOfInterfaces = a; +>thingOfInterfaces = a : A +>thingOfInterfaces : A | B | C +>a : A + +thingOfInterfaces = b; +>thingOfInterfaces = b : B +>thingOfInterfaces : A | B | C +>b : B + +thingOfInterfaces = c; +>thingOfInterfaces = c : C +>thingOfInterfaces : A | B | C +>c : C + +//////// + +type X = { +>X : X +>T : T + + xProp: T; +>xProp : T +>T : T +} + +type Y = { +>Y : Y +>T : T + + yProp: T; +>yProp : T +>T : T +} + +type Z = { +>Z : Z +>T : T + + zProp: T; +>zProp : T +>T : T +} + +declare const x: X; +>x : X +>X : X +>Foo : Foo + +declare const y: Y; +>y : Y +>Y : Y +>Foo : Foo + +declare const z: Z; +>z : Z +>Z : Z +>Foo : Foo + +declare let thingOfTypeAliases: X | Y | Z; +>thingOfTypeAliases : X | Y | Z +>X : X +>Bar : Bar +>Y : Y +>Baz : Baz +>Z : Z +>Kwah : Kwah + +thingOfTypeAliases = x; +>thingOfTypeAliases = x : X +>thingOfTypeAliases : X | Y | Z +>x : X + +thingOfTypeAliases = y; +>thingOfTypeAliases = y : Y +>thingOfTypeAliases : X | Y | Z +>y : Y + +thingOfTypeAliases = z; +>thingOfTypeAliases = z : Z +>thingOfTypeAliases : X | Y | Z +>z : Z + From 4f32691f3eca56122fb55042ff4bf27daf23ec4b Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Wed, 20 Jun 2018 23:13:05 -0700 Subject: [PATCH 3/6] Try to report errors on types who have matching type references. --- src/compiler/checker.ts | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ff1da5db911..08bd2815321 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10851,12 +10851,34 @@ namespace ts { } } if (reportErrors) { - const discriminantType = findMatchingDiscriminantType(source, target); - isRelatedTo(source, discriminantType || targetTypes[targetTypes.length - 1], /*reportErrors*/ true); + const bestMatchingType = + findMatchingDiscriminantType(source, target) || + findMatchingTypeReferenceOrTypeAliasReference(source, target); + + isRelatedTo(source, bestMatchingType || targetTypes[targetTypes.length - 1], /*reportErrors*/ true); } return Ternary.False; } + function findMatchingTypeReferenceOrTypeAliasReference(source: Type, unionTarget: UnionOrIntersectionType) { + if (source.flags & TypeFlags.Object && (source as ObjectType).objectFlags & (ObjectFlags.Reference | ObjectFlags.Anonymous) && unionTarget.flags & TypeFlags.Union) { + return find(unionTarget.types, t => { + if (t.flags & TypeFlags.Object) { + if ((source as ObjectType).objectFlags & (t as ObjectType).objectFlags & ObjectFlags.Reference) { + return (source as TypeReference).target === (t as TypeReference).target; + } + if ((source as ObjectType).objectFlags & (t as ObjectType).objectFlags & ObjectFlags.Anonymous) { + // TODO (drosen): Not sure why the following isn't sufficient. + // return !!(source as AnonymousType).aliasSymbol && (source as AnonymousType).aliasSymbol === (target as AnonymousType).aliasSymbol; + return false; + } + } + return false; + }); + } + } + + // Keep this up-to-date with the same logic within `getApparentTypeOfContextualType`, since they should behave similarly function findMatchingDiscriminantType(source: Type, target: UnionOrIntersectionType) { let match: Type | undefined; From 33b1e3a39a5be14eea8d353b61f9fa7d60c5d929 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Wed, 20 Jun 2018 23:13:26 -0700 Subject: [PATCH 4/6] Accepted baselines. --- tests/baselines/reference/for-of39.errors.txt | 8 ++++---- ...unionTypeErrorMessageTypeRefs01.errors.txt | 20 +++++++++++-------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/tests/baselines/reference/for-of39.errors.txt b/tests/baselines/reference/for-of39.errors.txt index 58b5f1ecdf1..62edda40de3 100644 --- a/tests/baselines/reference/for-of39.errors.txt +++ b/tests/baselines/reference/for-of39.errors.txt @@ -4,8 +4,8 @@ tests/cases/conformance/es6/for-ofStatements/for-of39.ts(1,19): error TS2345: Ar Types of parameters 'items' and 'items' are incompatible. Type 'ConcatArray<[string, boolean]>' is not assignable to type 'ConcatArray<[string, number] | [string, true]>'. Type '[string, boolean]' is not assignable to type '[string, number] | [string, true]'. - Type '[string, boolean]' is not assignable to type '[string, true]'. - Type 'boolean' is not assignable to type 'true'. + Type '[string, boolean]' is not assignable to type '[string, number]'. + Type 'boolean' is not assignable to type 'number'. ==== tests/cases/conformance/es6/for-ofStatements/for-of39.ts (1 errors) ==== @@ -17,8 +17,8 @@ tests/cases/conformance/es6/for-ofStatements/for-of39.ts(1,19): error TS2345: Ar !!! error TS2345: Types of parameters 'items' and 'items' are incompatible. !!! error TS2345: Type 'ConcatArray<[string, boolean]>' is not assignable to type 'ConcatArray<[string, number] | [string, true]>'. !!! error TS2345: Type '[string, boolean]' is not assignable to type '[string, number] | [string, true]'. -!!! error TS2345: Type '[string, boolean]' is not assignable to type '[string, true]'. -!!! error TS2345: Type 'boolean' is not assignable to type 'true'. +!!! error TS2345: Type '[string, boolean]' is not assignable to type '[string, number]'. +!!! error TS2345: Type 'boolean' is not assignable to type 'number'. for (var [k, v] of map) { k; v; diff --git a/tests/baselines/reference/unionTypeErrorMessageTypeRefs01.errors.txt b/tests/baselines/reference/unionTypeErrorMessageTypeRefs01.errors.txt index 63683c87610..e7918d098db 100644 --- a/tests/baselines/reference/unionTypeErrorMessageTypeRefs01.errors.txt +++ b/tests/baselines/reference/unionTypeErrorMessageTypeRefs01.errors.txt @@ -1,9 +1,11 @@ tests/cases/compiler/unionTypeErrorMessageTypeRefs01.ts(25,1): error TS2322: Type 'A' is not assignable to type 'A | B | C'. - Type 'A' is not assignable to type 'C'. - Property 'cProp' is missing in type 'A'. + Type 'A' is not assignable to type 'A'. + Type 'Foo' is not assignable to type 'Bar'. + Property 'bar' is missing in type 'Foo'. tests/cases/compiler/unionTypeErrorMessageTypeRefs01.ts(26,1): error TS2322: Type 'B' is not assignable to type 'A | B | C'. - Type 'B' is not assignable to type 'C'. - Property 'cProp' is missing in type 'B'. + Type 'B' is not assignable to type 'B'. + Type 'Foo' is not assignable to type 'Baz'. + Property 'baz' is missing in type 'Foo'. tests/cases/compiler/unionTypeErrorMessageTypeRefs01.ts(27,1): error TS2322: Type 'C' is not assignable to type 'A | B | C'. Type 'C' is not assignable to type 'C'. Type 'Foo' is not assignable to type 'Kwah'. @@ -48,13 +50,15 @@ tests/cases/compiler/unionTypeErrorMessageTypeRefs01.ts(50,1): error TS2322: Typ thingOfInterfaces = a; ~~~~~~~~~~~~~~~~~ !!! error TS2322: Type 'A' is not assignable to type 'A | B | C'. -!!! error TS2322: Type 'A' is not assignable to type 'C'. -!!! error TS2322: Property 'cProp' is missing in type 'A'. +!!! error TS2322: Type 'A' is not assignable to type 'A'. +!!! error TS2322: Type 'Foo' is not assignable to type 'Bar'. +!!! error TS2322: Property 'bar' is missing in type 'Foo'. thingOfInterfaces = b; ~~~~~~~~~~~~~~~~~ !!! error TS2322: Type 'B' is not assignable to type 'A | B | C'. -!!! error TS2322: Type 'B' is not assignable to type 'C'. -!!! error TS2322: Property 'cProp' is missing in type 'B'. +!!! error TS2322: Type 'B' is not assignable to type 'B'. +!!! error TS2322: Type 'Foo' is not assignable to type 'Baz'. +!!! error TS2322: Property 'baz' is missing in type 'Foo'. thingOfInterfaces = c; ~~~~~~~~~~~~~~~~~ !!! error TS2322: Type 'C' is not assignable to type 'A | B | C'. From 9df95fee79811c6d55e9906fb739c2e53aae3649 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 21 Jun 2018 13:13:26 -0700 Subject: [PATCH 5/6] Expand better error messages to anonymous object types with identical type aliases. --- src/compiler/checker.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 08bd2815321..92019736c3e 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10862,15 +10862,13 @@ namespace ts { function findMatchingTypeReferenceOrTypeAliasReference(source: Type, unionTarget: UnionOrIntersectionType) { if (source.flags & TypeFlags.Object && (source as ObjectType).objectFlags & (ObjectFlags.Reference | ObjectFlags.Anonymous) && unionTarget.flags & TypeFlags.Union) { - return find(unionTarget.types, t => { - if (t.flags & TypeFlags.Object) { - if ((source as ObjectType).objectFlags & (t as ObjectType).objectFlags & ObjectFlags.Reference) { - return (source as TypeReference).target === (t as TypeReference).target; + return find(unionTarget.types, target => { + if (target.flags & TypeFlags.Object) { + if ((source as ObjectType).objectFlags & (target as ObjectType).objectFlags & ObjectFlags.Reference) { + return (source as TypeReference).target === (target as TypeReference).target; } - if ((source as ObjectType).objectFlags & (t as ObjectType).objectFlags & ObjectFlags.Anonymous) { - // TODO (drosen): Not sure why the following isn't sufficient. - // return !!(source as AnonymousType).aliasSymbol && (source as AnonymousType).aliasSymbol === (target as AnonymousType).aliasSymbol; - return false; + if ((source as ObjectType).objectFlags & (target as ObjectType).objectFlags & ObjectFlags.Anonymous) { + return !!(source as AnonymousType).aliasSymbol && (source as AnonymousType).aliasSymbol === (target as AnonymousType).aliasSymbol; } } return false; From 6d755aa301cbd9442df1e51fd3536369e8002808 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 21 Jun 2018 13:13:50 -0700 Subject: [PATCH 6/6] Accepted baselines. --- ...unionTypeErrorMessageTypeRefs01.errors.txt | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/tests/baselines/reference/unionTypeErrorMessageTypeRefs01.errors.txt b/tests/baselines/reference/unionTypeErrorMessageTypeRefs01.errors.txt index e7918d098db..f5d3584501c 100644 --- a/tests/baselines/reference/unionTypeErrorMessageTypeRefs01.errors.txt +++ b/tests/baselines/reference/unionTypeErrorMessageTypeRefs01.errors.txt @@ -11,11 +11,13 @@ tests/cases/compiler/unionTypeErrorMessageTypeRefs01.ts(27,1): error TS2322: Typ Type 'Foo' is not assignable to type 'Kwah'. Property 'kwah' is missing in type 'Foo'. tests/cases/compiler/unionTypeErrorMessageTypeRefs01.ts(48,1): error TS2322: Type 'X' is not assignable to type 'X | Y | Z'. - Type 'X' is not assignable to type 'Z'. - Property 'zProp' is missing in type 'X'. + Type 'X' is not assignable to type 'X'. + Types of property 'xProp' are incompatible. + Type 'Foo' is not assignable to type 'Bar'. tests/cases/compiler/unionTypeErrorMessageTypeRefs01.ts(49,1): error TS2322: Type 'Y' is not assignable to type 'X | Y | Z'. - Type 'Y' is not assignable to type 'Z'. - Property 'zProp' is missing in type 'Y'. + Type 'Y' is not assignable to type 'Y'. + Types of property 'yProp' are incompatible. + Type 'Foo' is not assignable to type 'Baz'. tests/cases/compiler/unionTypeErrorMessageTypeRefs01.ts(50,1): error TS2322: Type 'Z' is not assignable to type 'X | Y | Z'. Type 'Z' is not assignable to type 'Z'. Types of property 'zProp' are incompatible. @@ -88,13 +90,15 @@ tests/cases/compiler/unionTypeErrorMessageTypeRefs01.ts(50,1): error TS2322: Typ thingOfTypeAliases = x; ~~~~~~~~~~~~~~~~~~ !!! error TS2322: Type 'X' is not assignable to type 'X | Y | Z'. -!!! error TS2322: Type 'X' is not assignable to type 'Z'. -!!! error TS2322: Property 'zProp' is missing in type 'X'. +!!! error TS2322: Type 'X' is not assignable to type 'X'. +!!! error TS2322: Types of property 'xProp' are incompatible. +!!! error TS2322: Type 'Foo' is not assignable to type 'Bar'. thingOfTypeAliases = y; ~~~~~~~~~~~~~~~~~~ !!! error TS2322: Type 'Y' is not assignable to type 'X | Y | Z'. -!!! error TS2322: Type 'Y' is not assignable to type 'Z'. -!!! error TS2322: Property 'zProp' is missing in type 'Y'. +!!! error TS2322: Type 'Y' is not assignable to type 'Y'. +!!! error TS2322: Types of property 'yProp' are incompatible. +!!! error TS2322: Type 'Foo' is not assignable to type 'Baz'. thingOfTypeAliases = z; ~~~~~~~~~~~~~~~~~~ !!! error TS2322: Type 'Z' is not assignable to type 'X | Y | Z'.