Add test case for readonly property assignment via type assertion per jakebailey feedback

Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-09-25 21:18:08 +00:00
parent 060a88a384
commit a6f8898ee5
5 changed files with 125 additions and 5 deletions
@@ -1,8 +1,9 @@
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(41,10): error TS2540: Cannot assign to 'prop' because it is a read-only property.
usedBeforeAssignedTypeAssertion.ts(47,12): error TS2454: Variable 'uninitialized' is used before being assigned.
==== usedBeforeAssignedTypeAssertion.ts (2 errors) ====
==== usedBeforeAssignedTypeAssertion.ts (3 errors) ====
// Test case for type assertion (angle bracket syntax) - assignment should not error
function testTypeAssertion() {
let x: number;
@@ -35,6 +36,21 @@ usedBeforeAssignedTypeAssertion.ts(34,12): error TS2454: Variable 'uninitialized
!!! error TS2588: Cannot assign to 'm' because it is a constant.
}
// Test case for readonly property assignment via type assertion - should error
function testReadonlyPropertyAssignment() {
interface ReadonlyInterface {
readonly prop: number;
}
let obj: ReadonlyInterface;
obj = { prop: 42 };
// Should error - cannot assign to readonly property, even through type assertion
(obj.prop as any) = 100;
~~~~
!!! error TS2540: Cannot assign to 'prop' because it is a read-only property.
}
// Test cases that should still produce errors for proper context
function shouldStillError() {
let uninitialized: number;
@@ -31,6 +31,19 @@ function testConstAssignment() {
(m as any) = 16; // Should error - cannot assign to const
}
// Test case for readonly property assignment via type assertion - should error
function testReadonlyPropertyAssignment() {
interface ReadonlyInterface {
readonly prop: number;
}
let obj: ReadonlyInterface;
obj = { prop: 42 };
// Should error - cannot assign to readonly property, even through type assertion
(obj.prop as any) = 100;
}
// Test cases that should still produce errors for proper context
function shouldStillError() {
let uninitialized: number;
@@ -64,6 +77,13 @@ function testConstAssignment() {
var m = 32;
m = 16; // Should error - cannot assign to const
}
// Test case for readonly property assignment via type assertion - should error
function testReadonlyPropertyAssignment() {
var obj;
obj = { prop: 42 };
// Should error - cannot assign to readonly property, even through type assertion
obj.prop = 100;
}
// Test cases that should still produce errors for proper context
function shouldStillError() {
var uninitialized;
@@ -56,13 +56,39 @@ function testConstAssignment() {
>m : Symbol(m, Decl(usedBeforeAssignedTypeAssertion.ts, 26, 9))
}
// Test case for readonly property assignment via type assertion - should error
function testReadonlyPropertyAssignment() {
>testReadonlyPropertyAssignment : Symbol(testReadonlyPropertyAssignment, Decl(usedBeforeAssignedTypeAssertion.ts, 28, 1))
interface ReadonlyInterface {
>ReadonlyInterface : Symbol(ReadonlyInterface, Decl(usedBeforeAssignedTypeAssertion.ts, 31, 43))
readonly prop: number;
>prop : Symbol(ReadonlyInterface.prop, Decl(usedBeforeAssignedTypeAssertion.ts, 32, 33))
}
let obj: ReadonlyInterface;
>obj : Symbol(obj, Decl(usedBeforeAssignedTypeAssertion.ts, 36, 7))
>ReadonlyInterface : Symbol(ReadonlyInterface, Decl(usedBeforeAssignedTypeAssertion.ts, 31, 43))
obj = { prop: 42 };
>obj : Symbol(obj, Decl(usedBeforeAssignedTypeAssertion.ts, 36, 7))
>prop : Symbol(prop, Decl(usedBeforeAssignedTypeAssertion.ts, 37, 11))
// Should error - cannot assign to readonly property, even through type assertion
(obj.prop as any) = 100;
>obj.prop : Symbol(ReadonlyInterface.prop, Decl(usedBeforeAssignedTypeAssertion.ts, 32, 33))
>obj : Symbol(obj, Decl(usedBeforeAssignedTypeAssertion.ts, 36, 7))
>prop : Symbol(ReadonlyInterface.prop, Decl(usedBeforeAssignedTypeAssertion.ts, 32, 33))
}
// Test cases that should still produce errors for proper context
function shouldStillError() {
>shouldStillError : Symbol(shouldStillError, Decl(usedBeforeAssignedTypeAssertion.ts, 28, 1))
>shouldStillError : Symbol(shouldStillError, Decl(usedBeforeAssignedTypeAssertion.ts, 41, 1))
let uninitialized: number;
>uninitialized : Symbol(uninitialized, Decl(usedBeforeAssignedTypeAssertion.ts, 32, 7))
>uninitialized : Symbol(uninitialized, Decl(usedBeforeAssignedTypeAssertion.ts, 45, 7))
return uninitialized; // Should error - never assigned
>uninitialized : Symbol(uninitialized, Decl(usedBeforeAssignedTypeAssertion.ts, 32, 7))
>uninitialized : Symbol(uninitialized, Decl(usedBeforeAssignedTypeAssertion.ts, 45, 7))
}
@@ -115,6 +115,51 @@ function testConstAssignment() {
> : ^^
}
// Test case for readonly property assignment via type assertion - should error
function testReadonlyPropertyAssignment() {
>testReadonlyPropertyAssignment : () => void
> : ^^^^^^^^^^
interface ReadonlyInterface {
readonly prop: number;
>prop : number
> : ^^^^^^
}
let obj: ReadonlyInterface;
>obj : ReadonlyInterface
> : ^^^^^^^^^^^^^^^^^
obj = { prop: 42 };
>obj = { prop: 42 } : { prop: number; }
> : ^^^^^^^^^^^^^^^^^
>obj : ReadonlyInterface
> : ^^^^^^^^^^^^^^^^^
>{ prop: 42 } : { prop: number; }
> : ^^^^^^^^^^^^^^^^^
>prop : number
> : ^^^^^^
>42 : 42
> : ^^
// Should error - cannot assign to readonly property, even through type assertion
(obj.prop as any) = 100;
>(obj.prop as any) = 100 : 100
> : ^^^
>(obj.prop as any) : any
> : ^^^
>obj.prop as any : any
> : ^^^
>obj.prop : any
> : ^^^
>obj : ReadonlyInterface
> : ^^^^^^^^^^^^^^^^^
>prop : any
> : ^^^
>100 : 100
> : ^^^
}
// Test cases that should still produce errors for proper context
function shouldStillError() {
>shouldStillError : () => number
@@ -30,6 +30,19 @@ function testConstAssignment() {
(m as any) = 16; // Should error - cannot assign to const
}
// Test case for readonly property assignment via type assertion - should error
function testReadonlyPropertyAssignment() {
interface ReadonlyInterface {
readonly prop: number;
}
let obj: ReadonlyInterface;
obj = { prop: 42 };
// Should error - cannot assign to readonly property, even through type assertion
(obj.prop as any) = 100;
}
// Test cases that should still produce errors for proper context
function shouldStillError() {
let uninitialized: number;