|
| 1 | +//// [tests/cases/compiler/spreadUnionPropOverride.ts] //// |
| 2 | + |
| 3 | +=== spreadUnionPropOverride.ts === |
| 4 | +// Repro from #62655 |
| 5 | +type Thing = { |
| 6 | +>Thing : Symbol(Thing, Decl(spreadUnionPropOverride.ts, 0, 0)) |
| 7 | + |
| 8 | + id: string; |
| 9 | +>id : Symbol(id, Decl(spreadUnionPropOverride.ts, 1, 14)) |
| 10 | + |
| 11 | + label: string; |
| 12 | +>label : Symbol(label, Decl(spreadUnionPropOverride.ts, 2, 15)) |
| 13 | + |
| 14 | +}; |
| 15 | + |
| 16 | +const things: Thing[] = []; |
| 17 | +>things : Symbol(things, Decl(spreadUnionPropOverride.ts, 6, 5)) |
| 18 | +>Thing : Symbol(Thing, Decl(spreadUnionPropOverride.ts, 0, 0)) |
| 19 | + |
| 20 | +function find(id: string): undefined | Thing { |
| 21 | +>find : Symbol(find, Decl(spreadUnionPropOverride.ts, 6, 27)) |
| 22 | +>id : Symbol(id, Decl(spreadUnionPropOverride.ts, 8, 14)) |
| 23 | +>Thing : Symbol(Thing, Decl(spreadUnionPropOverride.ts, 0, 0)) |
| 24 | + |
| 25 | + return things.find(thing => thing.id === id); |
| 26 | +>things.find : Symbol(Array.find, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --)) |
| 27 | +>things : Symbol(things, Decl(spreadUnionPropOverride.ts, 6, 5)) |
| 28 | +>find : Symbol(Array.find, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --)) |
| 29 | +>thing : Symbol(thing, Decl(spreadUnionPropOverride.ts, 9, 23)) |
| 30 | +>thing.id : Symbol(id, Decl(spreadUnionPropOverride.ts, 1, 14)) |
| 31 | +>thing : Symbol(thing, Decl(spreadUnionPropOverride.ts, 9, 23)) |
| 32 | +>id : Symbol(id, Decl(spreadUnionPropOverride.ts, 1, 14)) |
| 33 | +>id : Symbol(id, Decl(spreadUnionPropOverride.ts, 8, 14)) |
| 34 | +} |
| 35 | + |
| 36 | +declare function fun(thing: Thing): void; |
| 37 | +>fun : Symbol(fun, Decl(spreadUnionPropOverride.ts, 10, 1)) |
| 38 | +>thing : Symbol(thing, Decl(spreadUnionPropOverride.ts, 12, 21)) |
| 39 | +>Thing : Symbol(Thing, Decl(spreadUnionPropOverride.ts, 0, 0)) |
| 40 | + |
| 41 | +fun({ |
| 42 | +>fun : Symbol(fun, Decl(spreadUnionPropOverride.ts, 10, 1)) |
| 43 | + |
| 44 | + id: 'foo', |
| 45 | +>id : Symbol(id, Decl(spreadUnionPropOverride.ts, 14, 5)) |
| 46 | + |
| 47 | + ...find('foo') ?? { |
| 48 | +>find : Symbol(find, Decl(spreadUnionPropOverride.ts, 6, 27)) |
| 49 | + |
| 50 | + label: 'Foo', |
| 51 | +>label : Symbol(label, Decl(spreadUnionPropOverride.ts, 16, 23)) |
| 52 | + |
| 53 | + }, |
| 54 | +}); |
| 55 | + |
| 56 | +// Should not error when spreading a union where one type doesn't have the property |
| 57 | +const obj1 = { |
| 58 | +>obj1 : Symbol(obj1, Decl(spreadUnionPropOverride.ts, 22, 5)) |
| 59 | + |
| 60 | + x: 1, |
| 61 | +>x : Symbol(x, Decl(spreadUnionPropOverride.ts, 22, 14)) |
| 62 | + |
| 63 | + ...(Math.random() > 0.5 ? { y: 2 } : { y: 2, x: 3 }), |
| 64 | +>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) |
| 65 | +>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --)) |
| 66 | +>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) |
| 67 | +>y : Symbol(y, Decl(spreadUnionPropOverride.ts, 24, 31)) |
| 68 | +>y : Symbol(y, Decl(spreadUnionPropOverride.ts, 24, 42)) |
| 69 | +>x : Symbol(x, Decl(spreadUnionPropOverride.ts, 24, 48)) |
| 70 | + |
| 71 | +}; // OK - x might be overwritten |
| 72 | + |
| 73 | +// Should error when the property is in all constituents |
| 74 | +const obj2 = { |
| 75 | +>obj2 : Symbol(obj2, Decl(spreadUnionPropOverride.ts, 28, 5)) |
| 76 | + |
| 77 | + x: 1, |
| 78 | +>x : Symbol(x, Decl(spreadUnionPropOverride.ts, 28, 14)) |
| 79 | + |
| 80 | + ...(Math.random() > 0.5 ? { x: 2, y: 3 } : { x: 4, z: 5 }), |
| 81 | +>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) |
| 82 | +>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --)) |
| 83 | +>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) |
| 84 | +>x : Symbol(x, Decl(spreadUnionPropOverride.ts, 30, 31)) |
| 85 | +>y : Symbol(y, Decl(spreadUnionPropOverride.ts, 30, 37)) |
| 86 | +>x : Symbol(x, Decl(spreadUnionPropOverride.ts, 30, 48)) |
| 87 | +>z : Symbol(z, Decl(spreadUnionPropOverride.ts, 30, 54)) |
| 88 | + |
| 89 | +}; // Error - x is always overwritten |
| 90 | + |
| 91 | +// Should not error with optional property in union |
| 92 | +type Partial1 = { a: string; b?: number }; |
| 93 | +>Partial1 : Symbol(Partial1, Decl(spreadUnionPropOverride.ts, 31, 2)) |
| 94 | +>a : Symbol(a, Decl(spreadUnionPropOverride.ts, 34, 17)) |
| 95 | +>b : Symbol(b, Decl(spreadUnionPropOverride.ts, 34, 28)) |
| 96 | + |
| 97 | +type Partial2 = { a: string; c: boolean }; |
| 98 | +>Partial2 : Symbol(Partial2, Decl(spreadUnionPropOverride.ts, 34, 42)) |
| 99 | +>a : Symbol(a, Decl(spreadUnionPropOverride.ts, 35, 17)) |
| 100 | +>c : Symbol(c, Decl(spreadUnionPropOverride.ts, 35, 28)) |
| 101 | + |
| 102 | +declare const partial: Partial1 | Partial2; |
| 103 | +>partial : Symbol(partial, Decl(spreadUnionPropOverride.ts, 36, 13)) |
| 104 | +>Partial1 : Symbol(Partial1, Decl(spreadUnionPropOverride.ts, 31, 2)) |
| 105 | +>Partial2 : Symbol(Partial2, Decl(spreadUnionPropOverride.ts, 34, 42)) |
| 106 | + |
| 107 | +const obj3 = { |
| 108 | +>obj3 : Symbol(obj3, Decl(spreadUnionPropOverride.ts, 38, 5)) |
| 109 | + |
| 110 | + b: 42, |
| 111 | +>b : Symbol(b, Decl(spreadUnionPropOverride.ts, 38, 14)) |
| 112 | + |
| 113 | + ...partial, |
| 114 | +>partial : Symbol(partial, Decl(spreadUnionPropOverride.ts, 36, 13)) |
| 115 | + |
| 116 | +}; // OK - b is optional in Partial1 and missing in Partial2 |
| 117 | + |
| 118 | +// Should error when property is required in all types |
| 119 | +const obj4 = { |
| 120 | +>obj4 : Symbol(obj4, Decl(spreadUnionPropOverride.ts, 44, 5)) |
| 121 | + |
| 122 | + a: "test", |
| 123 | +>a : Symbol(a, Decl(spreadUnionPropOverride.ts, 44, 14)) |
| 124 | + |
| 125 | + ...partial, |
| 126 | +>partial : Symbol(partial, Decl(spreadUnionPropOverride.ts, 36, 13)) |
| 127 | + |
| 128 | +}; // Error - a is required in both types |
| 129 | + |
| 130 | +// More complex union case |
| 131 | +type A = { id: string; name: string }; |
| 132 | +>A : Symbol(A, Decl(spreadUnionPropOverride.ts, 47, 2)) |
| 133 | +>id : Symbol(id, Decl(spreadUnionPropOverride.ts, 50, 10)) |
| 134 | +>name : Symbol(name, Decl(spreadUnionPropOverride.ts, 50, 22)) |
| 135 | + |
| 136 | +type B = { name: string; age: number }; |
| 137 | +>B : Symbol(B, Decl(spreadUnionPropOverride.ts, 50, 38)) |
| 138 | +>name : Symbol(name, Decl(spreadUnionPropOverride.ts, 51, 10)) |
| 139 | +>age : Symbol(age, Decl(spreadUnionPropOverride.ts, 51, 24)) |
| 140 | + |
| 141 | +type C = { name: string }; |
| 142 | +>C : Symbol(C, Decl(spreadUnionPropOverride.ts, 51, 39)) |
| 143 | +>name : Symbol(name, Decl(spreadUnionPropOverride.ts, 52, 10)) |
| 144 | + |
| 145 | +declare const abc: A | B | C; |
| 146 | +>abc : Symbol(abc, Decl(spreadUnionPropOverride.ts, 54, 13)) |
| 147 | +>A : Symbol(A, Decl(spreadUnionPropOverride.ts, 47, 2)) |
| 148 | +>B : Symbol(B, Decl(spreadUnionPropOverride.ts, 50, 38)) |
| 149 | +>C : Symbol(C, Decl(spreadUnionPropOverride.ts, 51, 39)) |
| 150 | + |
| 151 | +const obj5 = { |
| 152 | +>obj5 : Symbol(obj5, Decl(spreadUnionPropOverride.ts, 56, 5)) |
| 153 | + |
| 154 | + id: "123", |
| 155 | +>id : Symbol(id, Decl(spreadUnionPropOverride.ts, 56, 14)) |
| 156 | + |
| 157 | + ...abc, |
| 158 | +>abc : Symbol(abc, Decl(spreadUnionPropOverride.ts, 54, 13)) |
| 159 | + |
| 160 | +}; // OK - id is only in A |
| 161 | + |
| 162 | +const obj6 = { |
| 163 | +>obj6 : Symbol(obj6, Decl(spreadUnionPropOverride.ts, 61, 5)) |
| 164 | + |
| 165 | + name: "test", |
| 166 | +>name : Symbol(name, Decl(spreadUnionPropOverride.ts, 61, 14)) |
| 167 | + |
| 168 | + ...abc, |
| 169 | +>abc : Symbol(abc, Decl(spreadUnionPropOverride.ts, 54, 13)) |
| 170 | + |
| 171 | +}; // Error - name is in all types |
| 172 | + |
0 commit comments