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
There is a practice to freeze instances in the constructor, right before returning them. But, unfortunately current implementation of the Readonly interface makes it impossible to do so, if there are private instance members of any kind, be it fields, accessors or methods.
classThingy{privatedoubleIt(){returnthis.value*2;}constructor(publicvalue: number){returnObject.freeze(this);// Property 'doubleIt' is missing in type 'Readonly<this>'}}
This enforces developers to use workarounds and local usage conventions.
Expected behavior:
No compile errors.
Actual behavior:
Compile error: Property 'squareIt' is missing in type 'Readonly<this>'
I see the referenced issue as related to this one, rather than duplicate of it.
You're talking about Object.freeze() returning Readonly? No, I think, that's fine. The strange part is that readonly can be applied to properties only, – that's strange IMO. I mean, methods too in principle may or may not be read-only. Not sure about getters and setters though
TypeScript Version: 2.8.1
Search Terms: Readonly, freeze, Object.freeze, private
Code
There is a practice to freeze instances in the constructor, right before
return
ing them. But, unfortunately current implementation of theReadonly
interface makes it impossible to do so, if there areprivate
instance members of any kind, be it fields, accessors or methods.This enforces developers to use workarounds and local usage conventions.
Expected behavior:
No compile errors.
Actual behavior:
Compile error:
Property 'squareIt' is missing in type 'Readonly<this>'
Playground Link
Related Issues:
#22315
The text was updated successfully, but these errors were encountered: