Skip to content

Commit a8e0042

Browse files
committed
Accept new baselines
1 parent 9771f42 commit a8e0042

File tree

3 files changed

+777
-0
lines changed

3 files changed

+777
-0
lines changed
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
//// [literalTypeWidening.ts]
2+
// Widening vs. non-widening literal types
3+
4+
function f1() {
5+
const c1 = "hello"; // Widening type "hello"
6+
let v1 = c1; // Type string
7+
const c2 = c1; // Widening type "hello"
8+
let v2 = c2; // Type string
9+
const c3: "hello" = "hello"; // Type "hello"
10+
let v3 = c3; // Type "hello"
11+
const c4: "hello" = c1; // Type "hello"
12+
let v4 = c4; // Type "hello"
13+
}
14+
15+
function f2(cond: boolean) {
16+
const c1 = cond ? "foo" : "bar"; // widening "foo" | widening "bar"
17+
const c2: "foo" | "bar" = c1; // "foo" | "bar"
18+
const c3 = cond ? c1 : c2; // "foo" | "bar"
19+
const c4 = cond ? c3 : "baz"; // "foo" | "bar" | widening "baz"
20+
const c5: "foo" | "bar" | "baz" = c4; // "foo" | "bar" | "baz"
21+
let v1 = c1; // string
22+
let v2 = c2; // "foo" | "bar"
23+
let v3 = c3; // "foo" | "bar"
24+
let v4 = c4; // string
25+
let v5 = c5; // "foo" | "bar" | "baz"
26+
}
27+
28+
function f3() {
29+
const c1 = 123; // Widening type 123
30+
let v1 = c1; // Type number
31+
const c2 = c1; // Widening type 123
32+
let v2 = c2; // Type number
33+
const c3: 123 = 123; // Type 123
34+
let v3 = c3; // Type 123
35+
const c4: 123 = c1; // Type 123
36+
let v4 = c4; // Type 123
37+
}
38+
39+
function f4(cond: boolean) {
40+
const c1 = cond ? 123 : 456; // widening 123 | widening 456
41+
const c2: 123 | 456 = c1; // 123 | 456
42+
const c3 = cond ? c1 : c2; // 123 | 456
43+
const c4 = cond ? c3 : 789; // 123 | 456 | widening 789
44+
const c5: 123 | 456 | 789 = c4; // 123 | 456 | 789
45+
let v1 = c1; // number
46+
let v2 = c2; // 123 | 456
47+
let v3 = c3; // 123 | 456
48+
let v4 = c4; // number
49+
let v5 = c5; // 123 | 456 | 789
50+
}
51+
52+
function f5() {
53+
const c1 = "foo";
54+
let v1 = c1;
55+
const c2: "foo" = "foo";
56+
let v2 = c2;
57+
const c3 = "foo" as "foo";
58+
let v3 = c3;
59+
const c4 = <"foo">"foo";
60+
let v4 = c4;
61+
}
62+
63+
// Repro from #10898
64+
65+
type FAILURE = "FAILURE";
66+
const FAILURE = "FAILURE";
67+
68+
type Result<T> = T | FAILURE;
69+
70+
function doWork<T>(): Result<T> {
71+
return FAILURE;
72+
}
73+
74+
function isSuccess<T>(result: Result<T>): result is T {
75+
return !isFailure(result);
76+
}
77+
78+
function isFailure<T>(result: Result<T>): result is FAILURE {
79+
return result === FAILURE;
80+
}
81+
82+
function increment(x: number): number {
83+
return x + 1;
84+
}
85+
86+
let result = doWork<number>();
87+
88+
if (isSuccess(result)) {
89+
increment(result);
90+
}
91+
92+
// Repro from #10898
93+
94+
type TestEvent = "onmouseover" | "onmouseout";
95+
96+
function onMouseOver(): TestEvent { return "onmouseover"; }
97+
98+
let x = onMouseOver();
99+
100+
//// [literalTypeWidening.js]
101+
// Widening vs. non-widening literal types
102+
function f1() {
103+
var c1 = "hello"; // Widening type "hello"
104+
var v1 = c1; // Type string
105+
var c2 = c1; // Widening type "hello"
106+
var v2 = c2; // Type string
107+
var c3 = "hello"; // Type "hello"
108+
var v3 = c3; // Type "hello"
109+
var c4 = c1; // Type "hello"
110+
var v4 = c4; // Type "hello"
111+
}
112+
function f2(cond) {
113+
var c1 = cond ? "foo" : "bar"; // widening "foo" | widening "bar"
114+
var c2 = c1; // "foo" | "bar"
115+
var c3 = cond ? c1 : c2; // "foo" | "bar"
116+
var c4 = cond ? c3 : "baz"; // "foo" | "bar" | widening "baz"
117+
var c5 = c4; // "foo" | "bar" | "baz"
118+
var v1 = c1; // string
119+
var v2 = c2; // "foo" | "bar"
120+
var v3 = c3; // "foo" | "bar"
121+
var v4 = c4; // string
122+
var v5 = c5; // "foo" | "bar" | "baz"
123+
}
124+
function f3() {
125+
var c1 = 123; // Widening type 123
126+
var v1 = c1; // Type number
127+
var c2 = c1; // Widening type 123
128+
var v2 = c2; // Type number
129+
var c3 = 123; // Type 123
130+
var v3 = c3; // Type 123
131+
var c4 = c1; // Type 123
132+
var v4 = c4; // Type 123
133+
}
134+
function f4(cond) {
135+
var c1 = cond ? 123 : 456; // widening 123 | widening 456
136+
var c2 = c1; // 123 | 456
137+
var c3 = cond ? c1 : c2; // 123 | 456
138+
var c4 = cond ? c3 : 789; // 123 | 456 | widening 789
139+
var c5 = c4; // 123 | 456 | 789
140+
var v1 = c1; // number
141+
var v2 = c2; // 123 | 456
142+
var v3 = c3; // 123 | 456
143+
var v4 = c4; // number
144+
var v5 = c5; // 123 | 456 | 789
145+
}
146+
function f5() {
147+
var c1 = "foo";
148+
var v1 = c1;
149+
var c2 = "foo";
150+
var v2 = c2;
151+
var c3 = "foo";
152+
var v3 = c3;
153+
var c4 = "foo";
154+
var v4 = c4;
155+
}
156+
var FAILURE = "FAILURE";
157+
function doWork() {
158+
return FAILURE;
159+
}
160+
function isSuccess(result) {
161+
return !isFailure(result);
162+
}
163+
function isFailure(result) {
164+
return result === FAILURE;
165+
}
166+
function increment(x) {
167+
return x + 1;
168+
}
169+
var result = doWork();
170+
if (isSuccess(result)) {
171+
increment(result);
172+
}
173+
function onMouseOver() { return "onmouseover"; }
174+
var x = onMouseOver();

0 commit comments

Comments
 (0)