Skip to content

Commit 060a88a

Browse files
Add test case for const assignment via type assertion per RyanCavanaugh feedback
Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
1 parent d02d79c commit 060a88a

File tree

5 files changed

+66
-5
lines changed

5 files changed

+66
-5
lines changed

tests/baselines/reference/usedBeforeAssignedTypeAssertion.errors.txt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
usedBeforeAssignedTypeAssertion.ts(28,12): error TS2454: Variable 'uninitialized' is used before being assigned.
1+
usedBeforeAssignedTypeAssertion.ts(28,6): error TS2588: Cannot assign to 'm' because it is a constant.
2+
usedBeforeAssignedTypeAssertion.ts(34,12): error TS2454: Variable 'uninitialized' is used before being assigned.
23

34

4-
==== usedBeforeAssignedTypeAssertion.ts (1 errors) ====
5+
==== usedBeforeAssignedTypeAssertion.ts (2 errors) ====
56
// Test case for type assertion (angle bracket syntax) - assignment should not error
67
function testTypeAssertion() {
78
let x: number;
@@ -26,6 +27,14 @@ usedBeforeAssignedTypeAssertion.ts(28,12): error TS2454: Variable 'uninitialized
2627
((nested as any) as unknown) = "test"; // Should not error
2728
}
2829

30+
// Test case for const assignment via type assertion - should error
31+
function testConstAssignment() {
32+
const m = 32;
33+
(m as any) = 16; // Should error - cannot assign to const
34+
~
35+
!!! error TS2588: Cannot assign to 'm' because it is a constant.
36+
}
37+
2938
// Test cases that should still produce errors for proper context
3039
function shouldStillError() {
3140
let uninitialized: number;

tests/baselines/reference/usedBeforeAssignedTypeAssertion.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ function testNested() {
2525
((nested as any) as unknown) = "test"; // Should not error
2626
}
2727

28+
// Test case for const assignment via type assertion - should error
29+
function testConstAssignment() {
30+
const m = 32;
31+
(m as any) = 16; // Should error - cannot assign to const
32+
}
33+
2834
// Test cases that should still produce errors for proper context
2935
function shouldStillError() {
3036
let uninitialized: number;
@@ -53,6 +59,11 @@ function testNested() {
5359
var nested;
5460
nested = "test"; // Should not error
5561
}
62+
// Test case for const assignment via type assertion - should error
63+
function testConstAssignment() {
64+
var m = 32;
65+
m = 16; // Should error - cannot assign to const
66+
}
5667
// Test cases that should still produce errors for proper context
5768
function shouldStillError() {
5869
var uninitialized;

tests/baselines/reference/usedBeforeAssignedTypeAssertion.symbols

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,24 @@ function testNested() {
4545
>nested : Symbol(nested, Decl(usedBeforeAssignedTypeAssertion.ts, 20, 7))
4646
}
4747

48+
// Test case for const assignment via type assertion - should error
49+
function testConstAssignment() {
50+
>testConstAssignment : Symbol(testConstAssignment, Decl(usedBeforeAssignedTypeAssertion.ts, 22, 1))
51+
52+
const m = 32;
53+
>m : Symbol(m, Decl(usedBeforeAssignedTypeAssertion.ts, 26, 9))
54+
55+
(m as any) = 16; // Should error - cannot assign to const
56+
>m : Symbol(m, Decl(usedBeforeAssignedTypeAssertion.ts, 26, 9))
57+
}
58+
4859
// Test cases that should still produce errors for proper context
4960
function shouldStillError() {
50-
>shouldStillError : Symbol(shouldStillError, Decl(usedBeforeAssignedTypeAssertion.ts, 22, 1))
61+
>shouldStillError : Symbol(shouldStillError, Decl(usedBeforeAssignedTypeAssertion.ts, 28, 1))
5162

5263
let uninitialized: number;
53-
>uninitialized : Symbol(uninitialized, Decl(usedBeforeAssignedTypeAssertion.ts, 26, 7))
64+
>uninitialized : Symbol(uninitialized, Decl(usedBeforeAssignedTypeAssertion.ts, 32, 7))
5465

5566
return uninitialized; // Should error - never assigned
56-
>uninitialized : Symbol(uninitialized, Decl(usedBeforeAssignedTypeAssertion.ts, 26, 7))
67+
>uninitialized : Symbol(uninitialized, Decl(usedBeforeAssignedTypeAssertion.ts, 32, 7))
5768
}

tests/baselines/reference/usedBeforeAssignedTypeAssertion.types

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,30 @@ function testNested() {
9191
> : ^^^^^^
9292
}
9393

94+
// Test case for const assignment via type assertion - should error
95+
function testConstAssignment() {
96+
>testConstAssignment : () => void
97+
> : ^^^^^^^^^^
98+
99+
const m = 32;
100+
>m : 32
101+
> : ^^
102+
>32 : 32
103+
> : ^^
104+
105+
(m as any) = 16; // Should error - cannot assign to const
106+
>(m as any) = 16 : 16
107+
> : ^^
108+
>(m as any) : any
109+
> : ^^^
110+
>m as any : any
111+
> : ^^^
112+
>m : any
113+
> : ^^^
114+
>16 : 16
115+
> : ^^
116+
}
117+
94118
// Test cases that should still produce errors for proper context
95119
function shouldStillError() {
96120
>shouldStillError : () => number

tests/cases/compiler/usedBeforeAssignedTypeAssertion.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ function testNested() {
2424
((nested as any) as unknown) = "test"; // Should not error
2525
}
2626

27+
// Test case for const assignment via type assertion - should error
28+
function testConstAssignment() {
29+
const m = 32;
30+
(m as any) = 16; // Should error - cannot assign to const
31+
}
32+
2733
// Test cases that should still produce errors for proper context
2834
function shouldStillError() {
2935
let uninitialized: number;

0 commit comments

Comments
 (0)