You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
interfaceIThing{x: number;}letreadonlyThing: Readonly<IThing>={x: 123};// This is an error, as expectedreadonlyThing.x=321;// Why is this not an error?letthing: 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.
This apparently isn't specific to Readonly<T>. It works with a regular interface:
interfaceIThing{x: number;}interfaceIReadonlyThing{readonlyx: number;}constreadonlyThing: IReadonlyThing={x: 123};// This is an error, as expectedreadonlyThing.x=321;// Why is this allowed without a type assertion?letthing: IThing=readonlyThing;thing.x=321;
TypeScript Version: 3.3.0-dev.20181207
Search Terms: Readonly interface assignable
Code
Expected behavior: Attempting to assign
Readonly<IThing>
toIThing
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
The text was updated successfully, but these errors were encountered: