-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Expand file tree
/
Copy pathclassPropertyInferenceFromBroaderTypeConst.types
More file actions
107 lines (91 loc) · 1.58 KB
/
classPropertyInferenceFromBroaderTypeConst.types
File metadata and controls
107 lines (91 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//// [tests/cases/compiler/classPropertyInferenceFromBroaderTypeConst.ts] ////
=== classPropertyInferenceFromBroaderTypeConst.ts ===
// Repro from GH#62264
// Class property should infer the wider declared type (AB), not the narrowed literal type ("A")
type AB = 'A' | 'B';
>AB : AB
> : ^^
const DEFAULT: AB = 'A';
>DEFAULT : AB
> : ^^
>'A' : "A"
> : ^^^
class C {
>C : C
> : ^
D = DEFAULT;
>D : AB
> : ^^
>DEFAULT : AB
> : ^^
method() {
>method : () => void
> : ^^^^^^^^^^
switch (this.D) {
>this.D : AB
> : ^^
>this : this
> : ^^^^
>D : AB
> : ^^
case 'A': break;
>'A' : "A"
> : ^^^
case 'B': break; // should not error
>'B' : "B"
> : ^^^
}
}
}
// D should be AB, not "A"
declare const c: C;
>c : C
> : ^
declare function expectAB(x: AB): void;
>expectAB : (x: AB) => void
> : ^ ^^ ^^^^^
>x : AB
> : ^^
expectAB(c.D); // ok
>expectAB(c.D) : void
> : ^^^^
>expectAB : (x: AB) => void
> : ^ ^^ ^^^^^
>c.D : AB
> : ^^
>c : C
> : ^
>D : AB
> : ^^
c.D = 'B'; // ok
>c.D = 'B' : "B"
> : ^^^
>c.D : AB
> : ^^
>c : C
> : ^
>D : AB
> : ^^
>'B' : "B"
> : ^^^
// Static property should work the same way
class D {
>D : D
> : ^
static SD = DEFAULT;
>SD : AB
> : ^^
>DEFAULT : AB
> : ^^
}
D.SD = 'B'; // ok
>D.SD = 'B' : "B"
> : ^^^
>D.SD : AB
> : ^^
>D : typeof D
> : ^^^^^^^^
>SD : AB
> : ^^
>'B' : "B"
> : ^^^