|
| 1 | +tests/cases/compiler/excessPropertyCheckWithUnions.ts(10,30): error TS2322: Type '{ tag: "T"; a1: string; }' is not assignable to type 'ADT'. |
| 2 | + Object literal may only specify known properties, and 'a1' does not exist in type '{ tag: "T"; }'. |
| 3 | +tests/cases/compiler/excessPropertyCheckWithUnions.ts(11,21): error TS2322: Type '{ tag: "A"; d20: 12; }' is not assignable to type 'ADT'. |
| 4 | + Object literal may only specify known properties, and 'd20' does not exist in type '{ tag: "A"; a1: string; }'. |
| 5 | +tests/cases/compiler/excessPropertyCheckWithUnions.ts(12,1): error TS2322: Type '{ tag: "D"; }' is not assignable to type 'ADT'. |
| 6 | + Type '{ tag: "D"; }' is not assignable to type '{ tag: "D"; d20: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20; }'. |
| 7 | + Property 'd20' is missing in type '{ tag: "D"; }'. |
| 8 | +tests/cases/compiler/excessPropertyCheckWithUnions.ts(33,28): error TS2322: Type '{ tag: "A"; x: string; extra: number; }' is not assignable to type 'Ambiguous'. |
| 9 | + Object literal may only specify known properties, and 'extra' does not exist in type 'Ambiguous'. |
| 10 | +tests/cases/compiler/excessPropertyCheckWithUnions.ts(34,26): error TS2322: Type '{ tag: "A"; y: number; extra: number; }' is not assignable to type 'Ambiguous'. |
| 11 | + Object literal may only specify known properties, and 'extra' does not exist in type 'Ambiguous'. |
| 12 | +tests/cases/compiler/excessPropertyCheckWithUnions.ts(39,1): error TS2322: Type '{ tag: "A"; }' is not assignable to type 'Ambiguous'. |
| 13 | + Type '{ tag: "A"; }' is not assignable to type '{ tag: "C"; }'. |
| 14 | + Types of property 'tag' are incompatible. |
| 15 | + Type '"A"' is not assignable to type '"C"'. |
| 16 | +tests/cases/compiler/excessPropertyCheckWithUnions.ts(40,1): error TS2322: Type '{ tag: "A"; z: true; }' is not assignable to type 'Ambiguous'. |
| 17 | + Type '{ tag: "A"; z: true; }' is not assignable to type '{ tag: "C"; }'. |
| 18 | + Types of property 'tag' are incompatible. |
| 19 | + Type '"A"' is not assignable to type '"C"'. |
| 20 | + |
| 21 | + |
| 22 | +==== tests/cases/compiler/excessPropertyCheckWithUnions.ts (7 errors) ==== |
| 23 | + type ADT = { |
| 24 | + tag: "A", |
| 25 | + a1: string |
| 26 | + } | { |
| 27 | + tag: "D", |
| 28 | + d20: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 29 | + } | { |
| 30 | + tag: "T", |
| 31 | + } |
| 32 | + let wrong: ADT = { tag: "T", a1: "extra" } |
| 33 | + ~~~~~~~~~~~ |
| 34 | +!!! error TS2322: Type '{ tag: "T"; a1: string; }' is not assignable to type 'ADT'. |
| 35 | +!!! error TS2322: Object literal may only specify known properties, and 'a1' does not exist in type '{ tag: "T"; }'. |
| 36 | + wrong = { tag: "A", d20: 12 } |
| 37 | + ~~~~~~~ |
| 38 | +!!! error TS2322: Type '{ tag: "A"; d20: 12; }' is not assignable to type 'ADT'. |
| 39 | +!!! error TS2322: Object literal may only specify known properties, and 'd20' does not exist in type '{ tag: "A"; a1: string; }'. |
| 40 | + wrong = { tag: "D" } |
| 41 | + ~~~~~ |
| 42 | +!!! error TS2322: Type '{ tag: "D"; }' is not assignable to type 'ADT'. |
| 43 | +!!! error TS2322: Type '{ tag: "D"; }' is not assignable to type '{ tag: "D"; d20: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20; }'. |
| 44 | +!!! error TS2322: Property 'd20' is missing in type '{ tag: "D"; }'. |
| 45 | + |
| 46 | + type Ambiguous = { |
| 47 | + tag: "A", |
| 48 | + x: string |
| 49 | + } | { |
| 50 | + tag: "A", |
| 51 | + y: number |
| 52 | + } | { |
| 53 | + tag: "B", |
| 54 | + z: boolean |
| 55 | + } | { |
| 56 | + tag: "C" |
| 57 | + } |
| 58 | + let amb: Ambiguous |
| 59 | + // no error for ambiguous tag, even when it could satisfy both constituents at once |
| 60 | + amb = { tag: "A", x: "hi" } |
| 61 | + amb = { tag: "A", y: 12 } |
| 62 | + amb = { tag: "A", x: "hi", y: 12 } |
| 63 | + |
| 64 | + // correctly error on excess property 'extra', even when ambiguous |
| 65 | + amb = { tag: "A", x: "hi", extra: 12 } |
| 66 | + ~~~~~~~~~ |
| 67 | +!!! error TS2322: Type '{ tag: "A"; x: string; extra: number; }' is not assignable to type 'Ambiguous'. |
| 68 | +!!! error TS2322: Object literal may only specify known properties, and 'extra' does not exist in type 'Ambiguous'. |
| 69 | + amb = { tag: "A", y: 12, extra: 12 } |
| 70 | + ~~~~~~~~~ |
| 71 | +!!! error TS2322: Type '{ tag: "A"; y: number; extra: number; }' is not assignable to type 'Ambiguous'. |
| 72 | +!!! error TS2322: Object literal may only specify known properties, and 'extra' does not exist in type 'Ambiguous'. |
| 73 | + |
| 74 | + // assignability errors still work. |
| 75 | + // But note that the error for `z: true` is the fallback one of reporting on |
| 76 | + // the last constituent since assignability error reporting can't find a single best discriminant either. |
| 77 | + amb = { tag: "A" } |
| 78 | + ~~~ |
| 79 | +!!! error TS2322: Type '{ tag: "A"; }' is not assignable to type 'Ambiguous'. |
| 80 | +!!! error TS2322: Type '{ tag: "A"; }' is not assignable to type '{ tag: "C"; }'. |
| 81 | +!!! error TS2322: Types of property 'tag' are incompatible. |
| 82 | +!!! error TS2322: Type '"A"' is not assignable to type '"C"'. |
| 83 | + amb = { tag: "A", z: true } |
| 84 | + ~~~ |
| 85 | +!!! error TS2322: Type '{ tag: "A"; z: true; }' is not assignable to type 'Ambiguous'. |
| 86 | +!!! error TS2322: Type '{ tag: "A"; z: true; }' is not assignable to type '{ tag: "C"; }'. |
| 87 | +!!! error TS2322: Types of property 'tag' are incompatible. |
| 88 | +!!! error TS2322: Type '"A"' is not assignable to type '"C"'. |
| 89 | + |
| 90 | + type Overlapping = |
| 91 | + | { a: 1, b: 1, first: string } |
| 92 | + | { a: 2, second: string } |
| 93 | + | { b: 3, third: string } |
| 94 | + let over: Overlapping |
| 95 | + |
| 96 | + // these two are not reported because there are two discriminant properties |
| 97 | + over = { a: 1, b: 1, first: "ok", second: "error" } |
| 98 | + over = { a: 1, b: 1, first: "ok", third: "error" } |
| 99 | + |
0 commit comments