mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-11 19:27:35 -06:00
Add test case for const assignment via type assertion per RyanCavanaugh feedback
Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
This commit is contained in:
parent
d02d79c199
commit
060a88a384
@ -1,7 +1,8 @@
|
||||
usedBeforeAssignedTypeAssertion.ts(28,12): error TS2454: Variable 'uninitialized' is used before being assigned.
|
||||
usedBeforeAssignedTypeAssertion.ts(28,6): error TS2588: Cannot assign to 'm' because it is a constant.
|
||||
usedBeforeAssignedTypeAssertion.ts(34,12): error TS2454: Variable 'uninitialized' is used before being assigned.
|
||||
|
||||
|
||||
==== usedBeforeAssignedTypeAssertion.ts (1 errors) ====
|
||||
==== usedBeforeAssignedTypeAssertion.ts (2 errors) ====
|
||||
// Test case for type assertion (angle bracket syntax) - assignment should not error
|
||||
function testTypeAssertion() {
|
||||
let x: number;
|
||||
@ -26,6 +27,14 @@ usedBeforeAssignedTypeAssertion.ts(28,12): error TS2454: Variable 'uninitialized
|
||||
((nested as any) as unknown) = "test"; // Should not error
|
||||
}
|
||||
|
||||
// Test case for const assignment via type assertion - should error
|
||||
function testConstAssignment() {
|
||||
const m = 32;
|
||||
(m as any) = 16; // Should error - cannot assign to const
|
||||
~
|
||||
!!! error TS2588: Cannot assign to 'm' because it is a constant.
|
||||
}
|
||||
|
||||
// Test cases that should still produce errors for proper context
|
||||
function shouldStillError() {
|
||||
let uninitialized: number;
|
||||
|
||||
@ -25,6 +25,12 @@ function testNested() {
|
||||
((nested as any) as unknown) = "test"; // Should not error
|
||||
}
|
||||
|
||||
// Test case for const assignment via type assertion - should error
|
||||
function testConstAssignment() {
|
||||
const m = 32;
|
||||
(m as any) = 16; // Should error - cannot assign to const
|
||||
}
|
||||
|
||||
// Test cases that should still produce errors for proper context
|
||||
function shouldStillError() {
|
||||
let uninitialized: number;
|
||||
@ -53,6 +59,11 @@ function testNested() {
|
||||
var nested;
|
||||
nested = "test"; // Should not error
|
||||
}
|
||||
// Test case for const assignment via type assertion - should error
|
||||
function testConstAssignment() {
|
||||
var m = 32;
|
||||
m = 16; // Should error - cannot assign to const
|
||||
}
|
||||
// Test cases that should still produce errors for proper context
|
||||
function shouldStillError() {
|
||||
var uninitialized;
|
||||
|
||||
@ -45,13 +45,24 @@ function testNested() {
|
||||
>nested : Symbol(nested, Decl(usedBeforeAssignedTypeAssertion.ts, 20, 7))
|
||||
}
|
||||
|
||||
// Test case for const assignment via type assertion - should error
|
||||
function testConstAssignment() {
|
||||
>testConstAssignment : Symbol(testConstAssignment, Decl(usedBeforeAssignedTypeAssertion.ts, 22, 1))
|
||||
|
||||
const m = 32;
|
||||
>m : Symbol(m, Decl(usedBeforeAssignedTypeAssertion.ts, 26, 9))
|
||||
|
||||
(m as any) = 16; // Should error - cannot assign to const
|
||||
>m : Symbol(m, Decl(usedBeforeAssignedTypeAssertion.ts, 26, 9))
|
||||
}
|
||||
|
||||
// Test cases that should still produce errors for proper context
|
||||
function shouldStillError() {
|
||||
>shouldStillError : Symbol(shouldStillError, Decl(usedBeforeAssignedTypeAssertion.ts, 22, 1))
|
||||
>shouldStillError : Symbol(shouldStillError, Decl(usedBeforeAssignedTypeAssertion.ts, 28, 1))
|
||||
|
||||
let uninitialized: number;
|
||||
>uninitialized : Symbol(uninitialized, Decl(usedBeforeAssignedTypeAssertion.ts, 26, 7))
|
||||
>uninitialized : Symbol(uninitialized, Decl(usedBeforeAssignedTypeAssertion.ts, 32, 7))
|
||||
|
||||
return uninitialized; // Should error - never assigned
|
||||
>uninitialized : Symbol(uninitialized, Decl(usedBeforeAssignedTypeAssertion.ts, 26, 7))
|
||||
>uninitialized : Symbol(uninitialized, Decl(usedBeforeAssignedTypeAssertion.ts, 32, 7))
|
||||
}
|
||||
|
||||
@ -91,6 +91,30 @@ function testNested() {
|
||||
> : ^^^^^^
|
||||
}
|
||||
|
||||
// Test case for const assignment via type assertion - should error
|
||||
function testConstAssignment() {
|
||||
>testConstAssignment : () => void
|
||||
> : ^^^^^^^^^^
|
||||
|
||||
const m = 32;
|
||||
>m : 32
|
||||
> : ^^
|
||||
>32 : 32
|
||||
> : ^^
|
||||
|
||||
(m as any) = 16; // Should error - cannot assign to const
|
||||
>(m as any) = 16 : 16
|
||||
> : ^^
|
||||
>(m as any) : any
|
||||
> : ^^^
|
||||
>m as any : any
|
||||
> : ^^^
|
||||
>m : any
|
||||
> : ^^^
|
||||
>16 : 16
|
||||
> : ^^
|
||||
}
|
||||
|
||||
// Test cases that should still produce errors for proper context
|
||||
function shouldStillError() {
|
||||
>shouldStillError : () => number
|
||||
|
||||
@ -24,6 +24,12 @@ function testNested() {
|
||||
((nested as any) as unknown) = "test"; // Should not error
|
||||
}
|
||||
|
||||
// Test case for const assignment via type assertion - should error
|
||||
function testConstAssignment() {
|
||||
const m = 32;
|
||||
(m as any) = 16; // Should error - cannot assign to const
|
||||
}
|
||||
|
||||
// Test cases that should still produce errors for proper context
|
||||
function shouldStillError() {
|
||||
let uninitialized: number;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user