|
| 1 | +//// [tests/cases/compiler/classPropertyInferenceFromBroaderTypeConst.ts] //// |
| 2 | + |
| 3 | +=== classPropertyInferenceFromBroaderTypeConst.ts === |
| 4 | +// Repro from GH#62264 |
| 5 | +// Class property should infer the wider declared type (AB), not the narrowed literal type ("A") |
| 6 | + |
| 7 | +type AB = 'A' | 'B'; |
| 8 | +>AB : Symbol(AB, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 0, 0)) |
| 9 | + |
| 10 | +const DEFAULT: AB = 'A'; |
| 11 | +>DEFAULT : Symbol(DEFAULT, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 5, 5)) |
| 12 | +>AB : Symbol(AB, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 0, 0)) |
| 13 | + |
| 14 | +class C { |
| 15 | +>C : Symbol(C, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 5, 24)) |
| 16 | + |
| 17 | + D = DEFAULT; |
| 18 | +>D : Symbol(C.D, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 7, 9)) |
| 19 | +>DEFAULT : Symbol(DEFAULT, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 5, 5)) |
| 20 | + |
| 21 | + method() { |
| 22 | +>method : Symbol(C.method, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 8, 16)) |
| 23 | + |
| 24 | + switch (this.D) { |
| 25 | +>this.D : Symbol(C.D, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 7, 9)) |
| 26 | +>this : Symbol(C, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 5, 24)) |
| 27 | +>D : Symbol(C.D, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 7, 9)) |
| 28 | + |
| 29 | + case 'A': break; |
| 30 | + case 'B': break; // should not error |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// D should be AB, not "A" |
| 36 | +declare const c: C; |
| 37 | +>c : Symbol(c, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 19, 13)) |
| 38 | +>C : Symbol(C, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 5, 24)) |
| 39 | + |
| 40 | +declare function expectAB(x: AB): void; |
| 41 | +>expectAB : Symbol(expectAB, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 19, 19)) |
| 42 | +>x : Symbol(x, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 20, 26)) |
| 43 | +>AB : Symbol(AB, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 0, 0)) |
| 44 | + |
| 45 | +expectAB(c.D); // ok |
| 46 | +>expectAB : Symbol(expectAB, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 19, 19)) |
| 47 | +>c.D : Symbol(C.D, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 7, 9)) |
| 48 | +>c : Symbol(c, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 19, 13)) |
| 49 | +>D : Symbol(C.D, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 7, 9)) |
| 50 | + |
| 51 | +c.D = 'B'; // ok |
| 52 | +>c.D : Symbol(C.D, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 7, 9)) |
| 53 | +>c : Symbol(c, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 19, 13)) |
| 54 | +>D : Symbol(C.D, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 7, 9)) |
| 55 | + |
| 56 | +// Static property should work the same way |
| 57 | +class D { |
| 58 | +>D : Symbol(D, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 22, 10)) |
| 59 | + |
| 60 | + static SD = DEFAULT; |
| 61 | +>SD : Symbol(D.SD, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 25, 9)) |
| 62 | +>DEFAULT : Symbol(DEFAULT, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 5, 5)) |
| 63 | +} |
| 64 | +D.SD = 'B'; // ok |
| 65 | +>D.SD : Symbol(D.SD, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 25, 9)) |
| 66 | +>D : Symbol(D, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 22, 10)) |
| 67 | +>SD : Symbol(D.SD, Decl(classPropertyInferenceFromBroaderTypeConst.ts, 25, 9)) |
| 68 | + |
0 commit comments