File: literalFreshnessPropagationOnNarrowing.types

package info (click to toggle)
node-typescript 4.9.5%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 533,908 kB
  • sloc: javascript: 2,018,330; makefile: 7; sh: 1
file content (175 lines) | stat: -rw-r--r-- 3,740 bytes parent folder | download | duplicates (3)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
=== tests/cases/compiler/literalFreshnessPropagationOnNarrowing.ts ===
function f1() {
>f1 : () => void

    let b = true;
>b : boolean
>true : true

    let obj = { b };
>obj : { b: boolean; }
>{ b } : { b: boolean; }
>b : boolean

    // Desired: OK
    // 3.0: OK
    // 3.1 as-is: OK
    // 3.1 minus widening propagation: error
    obj.b = false;
>obj.b = false : false
>obj.b : boolean
>obj : { b: boolean; }
>b : boolean
>false : false
}

function f2() {
>f2 : () => void

    type Element = (string | false);
>Element : string | false
>false : false

    type ElementOrArray = Element | Element[]; 
>ElementOrArray : (string | false) | (string | false)[]

    let el: Element = null as any;
>el : string | false
>null as any : any
>null : null

    let arr: Element[] = null as any;
>arr : (string | false)[]
>null as any : any
>null : null

    let elOrA: ElementOrArray = null as any;
>elOrA : (string | false) | (string | false)[]
>null as any : any
>null : null

    // Desired/actual: All OK
    let a1: ElementOrArray = el;
>a1 : (string | false) | (string | false)[]
>el : string | false

    let a2: ElementOrArray = arr;
>a2 : (string | false) | (string | false)[]
>arr : (string | false)[]

    let a3: ElementOrArray = [el];
>a3 : (string | false) | (string | false)[]
>[el] : (string | false)[]
>el : string | false

    let a4: ElementOrArray = Array.isArray(elOrA) ? elOrA : [elOrA];
>a4 : (string | false) | (string | false)[]
>Array.isArray(elOrA) ? elOrA : [elOrA] : (string | false)[]
>Array.isArray(elOrA) : boolean
>Array.isArray : (arg: any) => arg is any[]
>Array : ArrayConstructor
>isArray : (arg: any) => arg is any[]
>elOrA : (string | false) | (string | false)[]
>elOrA : (string | false)[]
>[elOrA] : (string | false)[]
>elOrA : string | false

    // Desired: OK
    // 3.0: Error
    // 3.1: OK
    let a5: ElementOrArray = [...Array.isArray(elOrA) ? elOrA : [elOrA]];
>a5 : (string | false) | (string | false)[]
>[...Array.isArray(elOrA) ? elOrA : [elOrA]] : (string | false)[]
>...Array.isArray(elOrA) ? elOrA : [elOrA] : string | false
>Array.isArray(elOrA) ? elOrA : [elOrA] : (string | false)[]
>Array.isArray(elOrA) : boolean
>Array.isArray : (arg: any) => arg is any[]
>Array : ArrayConstructor
>isArray : (arg: any) => arg is any[]
>elOrA : (string | false) | (string | false)[]
>elOrA : (string | false)[]
>[elOrA] : (string | false)[]
>elOrA : string | false
}

function f3() {
>f3 : () => void

    type XY = 'x' | 'y';
>XY : "x" | "y"

    const x: XY = 'x';
>x : "x" | "y"
>'x' : "x"

    let x2 = x;
>x2 : "x"
>x : "x"

    // Desired: OK (up for debate?)
    // 3.0: Error
    // 3.1 as-is: OK
    x2 = 'y';
>x2 = 'y' : "y"
>x2 : "x"
>'y' : "y"

    // Desired/actual: All OK
    let x3: XY = x;
>x3 : "x" | "y"
>x : "x"

    x3 = 'y';
>x3 = 'y' : "y"
>x3 : "x" | "y"
>'y' : "y"
}

function f4() {
>f4 : () => void

    const x: boolean = true;
>x : boolean
>true : true

    let x1 = x;
>x1 : boolean
>x : true

    // Desired: OK
    // 3.0: OK
    // 3.1: OK
    // 3.1 minus widening propagation: error
    x1 = false;
>x1 = false : false
>x1 : boolean
>false : false
}

function f5() {
>f5 : () => void

    type XY = 'x' | 'y';
>XY : "x" | "y"

    let arr: XY[] = ['x'];
>arr : ("x" | "y")[]
>['x'] : "x"[]
>'x' : "x"

    arr = ['y'];
>arr = ['y'] : "y"[]
>arr : ("x" | "y")[]
>['y'] : "y"[]
>'y' : "y"

    // Desired: OK
    // Error in all extant branches
    arr = [...['y']];
>arr = [...['y']] : string[]
>arr : ("x" | "y")[]
>[...['y']] : string[]
>...['y'] : string
>['y'] : string[]
>'y' : "y"
}