diff --git a/tests/baselines/reference/usedBeforeAssignedTypeAssertion.errors.txt b/tests/baselines/reference/usedBeforeAssignedTypeAssertion.errors.txt index 90453e70083..2bd93515b3f 100644 --- a/tests/baselines/reference/usedBeforeAssignedTypeAssertion.errors.txt +++ b/tests/baselines/reference/usedBeforeAssignedTypeAssertion.errors.txt @@ -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; diff --git a/tests/baselines/reference/usedBeforeAssignedTypeAssertion.js b/tests/baselines/reference/usedBeforeAssignedTypeAssertion.js index 1e72ae11dbd..d434d100a23 100644 --- a/tests/baselines/reference/usedBeforeAssignedTypeAssertion.js +++ b/tests/baselines/reference/usedBeforeAssignedTypeAssertion.js @@ -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; diff --git a/tests/baselines/reference/usedBeforeAssignedTypeAssertion.symbols b/tests/baselines/reference/usedBeforeAssignedTypeAssertion.symbols index e9ebf35a4ac..c4c3f7923fb 100644 --- a/tests/baselines/reference/usedBeforeAssignedTypeAssertion.symbols +++ b/tests/baselines/reference/usedBeforeAssignedTypeAssertion.symbols @@ -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)) } diff --git a/tests/baselines/reference/usedBeforeAssignedTypeAssertion.types b/tests/baselines/reference/usedBeforeAssignedTypeAssertion.types index 9b38ec1898d..da01dc01ab2 100644 --- a/tests/baselines/reference/usedBeforeAssignedTypeAssertion.types +++ b/tests/baselines/reference/usedBeforeAssignedTypeAssertion.types @@ -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 diff --git a/tests/cases/compiler/usedBeforeAssignedTypeAssertion.ts b/tests/cases/compiler/usedBeforeAssignedTypeAssertion.ts index a5de18c18bc..d187950789a 100644 --- a/tests/cases/compiler/usedBeforeAssignedTypeAssertion.ts +++ b/tests/cases/compiler/usedBeforeAssignedTypeAssertion.ts @@ -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;