From a6f8898ee59302e2557f554c06b45fa2b5fff654 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 25 Sep 2025 21:18:08 +0000 Subject: [PATCH] Add test case for readonly property assignment via type assertion per jakebailey feedback Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com> --- ...usedBeforeAssignedTypeAssertion.errors.txt | 20 ++++++++- .../usedBeforeAssignedTypeAssertion.js | 20 +++++++++ .../usedBeforeAssignedTypeAssertion.symbols | 32 +++++++++++-- .../usedBeforeAssignedTypeAssertion.types | 45 +++++++++++++++++++ .../usedBeforeAssignedTypeAssertion.ts | 13 ++++++ 5 files changed, 125 insertions(+), 5 deletions(-) diff --git a/tests/baselines/reference/usedBeforeAssignedTypeAssertion.errors.txt b/tests/baselines/reference/usedBeforeAssignedTypeAssertion.errors.txt index 2bd93515b3f..4cecead9a16 100644 --- a/tests/baselines/reference/usedBeforeAssignedTypeAssertion.errors.txt +++ b/tests/baselines/reference/usedBeforeAssignedTypeAssertion.errors.txt @@ -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; diff --git a/tests/baselines/reference/usedBeforeAssignedTypeAssertion.js b/tests/baselines/reference/usedBeforeAssignedTypeAssertion.js index d434d100a23..1f389b350d6 100644 --- a/tests/baselines/reference/usedBeforeAssignedTypeAssertion.js +++ b/tests/baselines/reference/usedBeforeAssignedTypeAssertion.js @@ -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; diff --git a/tests/baselines/reference/usedBeforeAssignedTypeAssertion.symbols b/tests/baselines/reference/usedBeforeAssignedTypeAssertion.symbols index c4c3f7923fb..5a7306690f2 100644 --- a/tests/baselines/reference/usedBeforeAssignedTypeAssertion.symbols +++ b/tests/baselines/reference/usedBeforeAssignedTypeAssertion.symbols @@ -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)) } diff --git a/tests/baselines/reference/usedBeforeAssignedTypeAssertion.types b/tests/baselines/reference/usedBeforeAssignedTypeAssertion.types index da01dc01ab2..7c8511bff5a 100644 --- a/tests/baselines/reference/usedBeforeAssignedTypeAssertion.types +++ b/tests/baselines/reference/usedBeforeAssignedTypeAssertion.types @@ -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 diff --git a/tests/cases/compiler/usedBeforeAssignedTypeAssertion.ts b/tests/cases/compiler/usedBeforeAssignedTypeAssertion.ts index d187950789a..77be4e15d79 100644 --- a/tests/cases/compiler/usedBeforeAssignedTypeAssertion.ts +++ b/tests/cases/compiler/usedBeforeAssignedTypeAssertion.ts @@ -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;