-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Expand file tree
/
Copy pathclassPropertyInferenceFromBroaderTypeConst.js
More file actions
55 lines (46 loc) · 1.2 KB
/
classPropertyInferenceFromBroaderTypeConst.js
File metadata and controls
55 lines (46 loc) · 1.2 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
//// [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';
const DEFAULT: AB = 'A';
class C {
D = DEFAULT;
method() {
switch (this.D) {
case 'A': break;
case 'B': break; // should not error
}
}
}
// D should be AB, not "A"
declare const c: C;
declare function expectAB(x: AB): void;
expectAB(c.D); // ok
c.D = 'B'; // ok
// Static property should work the same way
class D {
static SD = DEFAULT;
}
D.SD = 'B'; // ok
//// [classPropertyInferenceFromBroaderTypeConst.js]
"use strict";
// Repro from GH#62264
// Class property should infer the wider declared type (AB), not the narrowed literal type ("A")
const DEFAULT = 'A';
class C {
D = DEFAULT;
method() {
switch (this.D) {
case 'A': break;
case 'B': break; // should not error
}
}
}
expectAB(c.D); // ok
c.D = 'B'; // ok
// Static property should work the same way
class D {
static SD = DEFAULT;
}
D.SD = 'B'; // ok