forked from microsoft/TypeScript
/
consistentUnionSubtypeReduction.js
100 lines (88 loc) · 1.84 KB
/
consistentUnionSubtypeReduction.js
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
//// [consistentUnionSubtypeReduction.ts]
// https://github.com/microsoft/TypeScript/issues/31155
declare const MyArray: {
isArray<T>(arg: T | {}): arg is T extends readonly any[] ? readonly any[] : any[];
};
declare const a: readonly string[] | string;
declare const b: string[] | string;
declare const c: unknown;
if (MyArray.isArray(a)) {
a; // readonly string[]
}
else {
a; // string
}
a; // readonly string[] | string;
if (MyArray.isArray(b)) {
b; // string[] | string;
}
else {
b; // string
}
b; // string[] | string;
if (MyArray.isArray(c)) {
c; // any[]
}
function f<T>(x: T) {
const a: readonly T[] | string = null!;
const b: T[] | string = null!;
const c: T = null!;
if (MyArray.isArray(a)) {
a; // readonly T[]
}
else {
a; // string
}
a; // readonly T[] | string;
if (MyArray.isArray(b)) {
b; // T[]
}
else {
b; // string
}
b;
if (MyArray.isArray(c)) {
c; // T & (T extends readonly any[] ? readonly any[] : any[])
}
}
//// [consistentUnionSubtypeReduction.js]
// https://github.com/microsoft/TypeScript/issues/31155
if (MyArray.isArray(a)) {
a; // readonly string[]
}
else {
a; // string
}
a; // readonly string[] | string;
if (MyArray.isArray(b)) {
b; // string[] | string;
}
else {
b; // string
}
b; // string[] | string;
if (MyArray.isArray(c)) {
c; // any[]
}
function f(x) {
var a = null;
var b = null;
var c = null;
if (MyArray.isArray(a)) {
a; // readonly T[]
}
else {
a; // string
}
a; // readonly T[] | string;
if (MyArray.isArray(b)) {
b; // T[]
}
else {
b; // string
}
b;
if (MyArray.isArray(c)) {
c; // T & (T extends readonly any[] ? readonly any[] : any[])
}
}