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

Readonly<T> should not be assignable to T without a type assertion #28906

Closed
octogonz opened this issue Dec 7, 2018 · 5 comments
Closed

Readonly<T> should not be assignable to T without a type assertion #28906

octogonz opened this issue Dec 7, 2018 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@octogonz
Copy link

octogonz commented Dec 7, 2018

TypeScript Version: 3.3.0-dev.20181207

Search Terms: Readonly interface assignable

Code

interface IThing {
    x: number;
}

let readonlyThing: Readonly<IThing> = { x: 123 };

// This is an error, as expected
readonlyThing.x = 321; 

// Why is this not an error?
let thing: IThing = readonlyThing;
thing.x = 321;

Expected behavior: Attempting to assign Readonly<IThing> to IThing should result in an error.

Actual behavior: The code compiles without any error, allowing "x" to be assigned even though it was intended to be readonly.

Playground Link: http://www.typescriptlang.org/play/#src=%0D%0Ainterface%20IThing%20%7B%0D%0A%20%20%20%20x%3A%20number%3B%0D%0A%7D%0D%0A%0D%0Alet%20readonlyThing%3A%20Readonly%3CIThing%3E%20%3D%20%7B%20x%3A%20123%20%7D%3B%0D%0A%0D%0A%2F%2F%20This%20is%20an%20error%2C%20as%20expected%0D%0AreadonlyThing.x%20%3D%20321%3B%20%0D%0A%0D%0A%2F%2F%20Why%20is%20this%20not%20an%20error%3F%0D%0Alet%20thing%3A%20IThing%20%3D%20readonlyThing%3B%0D%0Athing.x%20%3D%20321%3B%0D%0A

Related Issues:
#26864
#13002
#12542

@octogonz
Copy link
Author

octogonz commented Dec 7, 2018

This apparently isn't specific to Readonly<T>. It works with a regular interface:

interface IThing {
    x: number;
}

interface IReadonlyThing {
    readonly x: number;
}

const readonlyThing: IReadonlyThing = { x: 123 };

// This is an error, as expected
readonlyThing.x = 321; 

// Why is this allowed without a type assertion?
let thing: IThing = readonlyThing;
thing.x = 321;

@jack-williams
Copy link
Collaborator

I think this is related: #13347

@weswigham
Copy link
Member

Aye duplicate of #13347 - we're considering a kind of --strictReadonly in the future to opt in to strict behavior here.

@weswigham weswigham added the Duplicate An existing issue was already created label Dec 7, 2018
@octogonz
Copy link
Author

octogonz commented Dec 8, 2018

Thanks! I'll close this issue then.

@octogonz octogonz closed this as completed Dec 8, 2018
@typescript-bot
Copy link
Collaborator

This issue has been marked as a duplicate and has seen no activity in the last day. It has been closed automatic house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants