Skip to content

Commit 9517109

Browse files
committed
refactor + add new baseline tests
1 parent ba95739 commit 9517109

15 files changed

+1080
-2
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21472,7 +21472,7 @@ namespace ts {
2147221472
if (isLiteralType(operandType) && !(operandType.flags & (TypeFlags.EnumLike | TypeFlags.BooleanLike | TypeFlags.Undefined))) {
2147321473
error(node.operand, Diagnostics.The_literal_type_0_cannot_be_modified, typeToString(operandType));
2147421474
}
21475-
}
21475+
}
2147621476
operandType = checkExpression(node.operand);
2147721477

2147821478
if (operandType === silentNeverType) {

src/compiler/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3768,7 +3768,7 @@ namespace ts {
37683768
}
37693769

37703770
export function isCompoundAssignmentOperator(token: SyntaxKind): boolean {
3771-
return token === SyntaxKind.PlusEqualsToken
3771+
return token === SyntaxKind.PlusEqualsToken
37723772
|| token === SyntaxKind.MinusEqualsToken
37733773
|| token === SyntaxKind.AsteriskAsteriskEqualsToken
37743774
|| token === SyntaxKind.AsteriskEqualsToken
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
tests/cases/conformance/types/literal/enumLiteralTypes1.ts(44,5): error TS2736: The literal type 'YesNo' cannot be modified.
2+
3+
4+
==== tests/cases/conformance/types/literal/enumLiteralTypes1.ts (1 errors) ====
5+
const enum Choice { Unknown, Yes, No };
6+
7+
type YesNo = Choice.Yes | Choice.No;
8+
type NoYes = Choice.No | Choice.Yes;
9+
type UnknownYesNo = Choice.Unknown | Choice.Yes | Choice.No;
10+
11+
function f1() {
12+
var a: YesNo;
13+
var a: NoYes;
14+
var a: Choice.Yes | Choice.No;
15+
var a: Choice.No | Choice.Yes;
16+
}
17+
18+
function f2(a: YesNo, b: UnknownYesNo, c: Choice) {
19+
b = a;
20+
c = a;
21+
c = b;
22+
}
23+
24+
function f3(a: Choice.Yes, b: YesNo) {
25+
var x = a + b;
26+
var x = a - b;
27+
var x = a * b;
28+
var x = a / b;
29+
var x = a % b;
30+
var x = a | b;
31+
var x = a & b;
32+
var x = a ^ b;
33+
var x = -b;
34+
var x = ~b;
35+
var y = a == b;
36+
var y = a != b;
37+
var y = a === b;
38+
var y = a !== b;
39+
var y = a > b;
40+
var y = a < b;
41+
var y = a >= b;
42+
var y = a <= b;
43+
var y = !b;
44+
}
45+
46+
function f4(a: Choice.Yes, b: YesNo) {
47+
a++;
48+
b++;
49+
~
50+
!!! error TS2736: The literal type 'YesNo' cannot be modified.
51+
}
52+
53+
declare function g(x: Choice.Yes): string;
54+
declare function g(x: Choice.No): boolean;
55+
declare function g(x: Choice): number;
56+
57+
function f5(a: YesNo, b: UnknownYesNo, c: Choice) {
58+
var z1 = g(Choice.Yes);
59+
var z2 = g(Choice.No);
60+
var z3 = g(a);
61+
var z4 = g(b);
62+
var z5 = g(c);
63+
}
64+
65+
function assertNever(x: never): never {
66+
throw new Error("Unexpected value");
67+
}
68+
69+
function f10(x: YesNo) {
70+
switch (x) {
71+
case Choice.Yes: return "true";
72+
case Choice.No: return "false";
73+
}
74+
}
75+
76+
function f11(x: YesNo) {
77+
switch (x) {
78+
case Choice.Yes: return "true";
79+
case Choice.No: return "false";
80+
}
81+
return assertNever(x);
82+
}
83+
84+
function f12(x: UnknownYesNo) {
85+
if (x) {
86+
x;
87+
}
88+
else {
89+
x;
90+
}
91+
}
92+
93+
function f13(x: UnknownYesNo) {
94+
if (x === Choice.Yes) {
95+
x;
96+
}
97+
else {
98+
x;
99+
}
100+
}
101+
102+
type Item =
103+
{ kind: Choice.Yes, a: string } |
104+
{ kind: Choice.No, b: string };
105+
106+
function f20(x: Item) {
107+
switch (x.kind) {
108+
case Choice.Yes: return x.a;
109+
case Choice.No: return x.b;
110+
}
111+
}
112+
113+
function f21(x: Item) {
114+
switch (x.kind) {
115+
case Choice.Yes: return x.a;
116+
case Choice.No: return x.b;
117+
}
118+
return assertNever(x);
119+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
tests/cases/conformance/types/literal/numericLiteralTypes1.ts(48,5): error TS2736: The literal type '1' cannot be modified.
2+
tests/cases/conformance/types/literal/numericLiteralTypes1.ts(49,5): error TS2736: The literal type '0 | 1 | 2' cannot be modified.
3+
4+
5+
==== tests/cases/conformance/types/literal/numericLiteralTypes1.ts (2 errors) ====
6+
type A1 = 1;
7+
type A2 = 1.0;
8+
type A3 = 1e0;
9+
type A4 = 10e-1;
10+
type A5 = 1 | 1.0 | 1e0 | 10e-1;
11+
12+
function f1() {
13+
var a: A1 = 1;
14+
var a: A2 = 1;
15+
var a: A3 = 1;
16+
var a: A4 = 1;
17+
var a: A5 = 1;
18+
}
19+
20+
type B1 = -1 | 0 | 1;
21+
type B2 = 1 | 0 | -1;
22+
type B3 = 0 | -1 | 1;
23+
24+
function f2() {
25+
var b: B1 = -1;
26+
var b: B2 = 0;
27+
var b: B3 = 1;
28+
}
29+
30+
function f3(a: 1, b: 0 | 1 | 2) {
31+
var x = a + b;
32+
var x = a - b;
33+
var x = a * b;
34+
var x = a / b;
35+
var x = a % b;
36+
var x = a | b;
37+
var x = a & b;
38+
var x = a ^ b;
39+
var x = -b;
40+
var x = ~b;
41+
var y = a == b;
42+
var y = a != b;
43+
var y = a === b;
44+
var y = a !== b;
45+
var y = a > b;
46+
var y = a < b;
47+
var y = a >= b;
48+
var y = a <= b;
49+
var y = !b;
50+
}
51+
52+
function f4(a: 1, b: 0 | 1 | 2) {
53+
a++;
54+
~
55+
!!! error TS2736: The literal type '1' cannot be modified.
56+
b++;
57+
~
58+
!!! error TS2736: The literal type '0 | 1 | 2' cannot be modified.
59+
}
60+
61+
declare function g(x: 0): string;
62+
declare function g(x: 1): boolean;
63+
declare function g(x: number): number;
64+
65+
function f5(a: 1, b: 0 | 1 | 2) {
66+
var z1 = g(0);
67+
var z2 = g(1);
68+
var z3 = g(2);
69+
var z4 = g(a);
70+
var z5 = g(b);
71+
}
72+
73+
function assertNever(x: never): never {
74+
throw new Error("Unexpected value");
75+
}
76+
77+
type Tag = 0 | 1 | 2;
78+
79+
function f10(x: Tag) {
80+
switch (x) {
81+
case 0: return "a";
82+
case 1: return "b";
83+
case 2: return "c";
84+
}
85+
}
86+
87+
function f11(x: Tag) {
88+
switch (x) {
89+
case 0: return "a";
90+
case 1: return "b";
91+
case 2: return "c";
92+
}
93+
return assertNever(x);
94+
}
95+
96+
function f12(x: Tag) {
97+
if (x) {
98+
x;
99+
}
100+
else {
101+
x;
102+
}
103+
}
104+
105+
function f13(x: Tag) {
106+
if (x === 0 || x === 2) {
107+
x;
108+
}
109+
else {
110+
x;
111+
}
112+
}
113+
114+
function f14(x: 0 | 1 | 2, y: string) {
115+
var a = x && y;
116+
var b = x || y;
117+
}
118+
119+
function f15(x: 0 | false, y: 1 | "one") {
120+
var a = x && y;
121+
var b = y && x;
122+
var c = x || y;
123+
var d = y || x;
124+
var e = !x;
125+
var f = !y;
126+
}
127+
128+
type Item =
129+
{ kind: 0, a: string } |
130+
{ kind: 1, b: string } |
131+
{ kind: 2, c: string };
132+
133+
function f20(x: Item) {
134+
switch (x.kind) {
135+
case 0: return x.a;
136+
case 1: return x.b;
137+
case 2: return x.c;
138+
}
139+
}
140+
141+
function f21(x: Item) {
142+
switch (x.kind) {
143+
case 0: return x.a;
144+
case 1: return x.b;
145+
case 2: return x.c;
146+
}
147+
return assertNever(x);
148+
}

0 commit comments

Comments
 (0)