-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
how can i strip readonly via mapped types? #13000
Comments
I'm running into exactly the same issue. |
Forget about it, readonly properties are useless due to #13002. Focus on something more rewarding. |
I don't know whether this will be of any use to you but: interface Point {
readonly x: number;
readonly y: number;
}
declare const p: Point;
declare function stripReadonly<T>(x: Readonly<T>): T;
stripReadonly(p) // { x: number, y: number } See also: #12589 Edit: removing the |
It would have been a good choice it we chose mutable version by default, so that only in few places had we use So if I go this route I would have Readonly<...> everyehere. Which is a questionable return for investment. |
@Aleksey-Bykov Not necessarily. Why can't you define them as following instead? interface WritablePoint {
x: number;
y: number;
}
type Point = Readonly<WritablePoint>; and then use |
@RReverser read my answer above, tldr: when readonly is a default choice, so it should be encoded with a shorter signature |
@Aleksey-Bykov Well, you define type just once anyway and in all the other places you can still use it with short name. |
@RReverser you are right, should have read you more closely, yes i guess it might be an option, need to sleep over it |
@RReverser i tried it, major problem is that type declarations cannot be inherited like interfaces type Point = Readonly<WritablePoint>
type Size = Readonly<WritableSize>
interface Box extends Point, Size { // <-- type error
} the other way around is also possible but it looks awkward interface WritableBox extends WritablePoint, WritableSize {
}
type Box = Readonly<WritableBox> |
interface Box extends Point, Size { // <-- type error Think type & / intersection solve it 🌹 |
Agree with @basarat, basically for inheriting type you'll need |
nightly build of Dec 17
given
i need a writable version of it
where
Writable
isThe text was updated successfully, but these errors were encountered: