Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New --enforceReadonly compiler option to enforce read-only semantics in type relations #58296

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 23 additions & 10 deletions tests/baselines/reference/enforceReadonly1.errors.txt
Expand Up @@ -6,16 +6,16 @@ enforceReadonly1.ts(17,5): error TS2322: Type 'T' is not assignable to type 'Mut
enforceReadonly1.ts(18,5): error TS2322: Type 'Readonly<T>' is not assignable to type 'Mutable<T>'.
enforceReadonly1.ts(20,5): error TS2322: Type 'Readonly<T>' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to 'Readonly<T>'.
enforceReadonly1.ts(31,11): error TS2430: Interface 'D1' incorrectly extends interface 'B1'.
enforceReadonly1.ts(29,5): error TS2322: Type '{ readonly foo: () => void; }' is not assignable to type '{ foo: () => void; }'.
Property 'foo' is 'readonly' in the source but not in the target.
enforceReadonly1.ts(44,11): error TS2430: Interface 'D1' incorrectly extends interface 'B1'.
Property 'x' is 'readonly' in the source but not in the target.
enforceReadonly1.ts(40,11): error TS2430: Interface 'D2' incorrectly extends interface 'B2'.
enforceReadonly1.ts(53,11): error TS2430: Interface 'D2' incorrectly extends interface 'B2'.
Property 'x' is 'readonly' in the source but not in the target.
enforceReadonly1.ts(48,7): error TS2415: Class 'D3' incorrectly extends base class 'B3'.
enforceReadonly1.ts(61,7): error TS2415: Class 'D3' incorrectly extends base class 'B3'.
Property 'x' is 'readonly' in the source but not in the target.
enforceReadonly1.ts(56,7): error TS2415: Class 'D4' incorrectly extends base class 'B4'.
Property 'foo' is 'readonly' in the source but not in the target.
enforceReadonly1.ts(95,3): error TS2540: Cannot assign to 'value' because it is a read-only property.
enforceReadonly1.ts(97,5): error TS2322: Type 'ImmutableValue<string>' is not assignable to type 'MutableValue<string>'.
enforceReadonly1.ts(108,3): error TS2540: Cannot assign to 'value' because it is a read-only property.
enforceReadonly1.ts(110,5): error TS2322: Type 'ImmutableValue<string>' is not assignable to type 'MutableValue<string>'.
Property 'value' is 'readonly' in the source but not in the target.


Expand Down Expand Up @@ -58,6 +58,22 @@ enforceReadonly1.ts(97,5): error TS2322: Type 'ImmutableValue<string>' is not as
rt = tt;
}

// A read-only property is assignable to a property declared as a method
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might be good to add rationale behind this in the PR's description


function f3(m: { foo(): void }, p: { foo: () => void }, r: { readonly foo: () => void }) {
m = r;
p = r; // Error
~
!!! error TS2322: Type '{ readonly foo: () => void; }' is not assignable to type '{ foo: () => void; }'.
!!! error TS2322: Property 'foo' is 'readonly' in the source but not in the target.
}

type Paths = string[] & { __brand__: "Paths" };

function f4(rp: Readonly<Paths>, rs: Readonly<string[]>) {
rs = rp;
}

// A derived interface may not change property from mutable to read-only

interface B1 {
Expand Down Expand Up @@ -99,9 +115,6 @@ enforceReadonly1.ts(97,5): error TS2322: Type 'ImmutableValue<string>' is not as
}

class D4 extends B4 { // Error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this // Error comment is now outdated

~~
!!! error TS2415: Class 'D4' incorrectly extends base class 'B4'.
!!! error TS2415: Property 'foo' is 'readonly' in the source but not in the target.
readonly foo = () => {}
}

Expand Down