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
{{ message }}
Repository navigation
Allow identifying readonly properties in mapped types #31581
I would like to see some syntactic sugar similar to (not intended to be a full implementation):
typeWritable<T>={[PinkeyofT]: isreadonlyT[P] ? never : T[P]}
In my case, I would like to define a hyperscript-style function which accepts a series of attributes for the creation of HTML elements. For this, I reuse the interfaces that TypeScript defines, such as HTMLAnchorElement, but obviously readonly properties (among others) need to be excluded.
I'd be interested in more streamlined manipulation of modifiers in mapped types. Detecting readonly and optional properties is indeed a bunch of hoop jumping, and then if you want to selectively alter the modifiers, you need to split the mapping into pieces and intersect them:
typeSelectivePartial<T,KextendskeyofT>=Partial<Pick<T,K>>&Required<Pick<T,Exclude<keyofT,K>>>extends
infer U ? {[PinkeyofU]: U[P]} : never;typeFoo=SelectivePartial<{a: string,b: number,c?: boolean},'b'>// type Foo = { b?: number | undefined; a: string; c: boolean; }
It would be a lot nicer to essentially read and selectively write modifiers inside the mapping directly. (Note the word "selectively"; the current support for altering modifiers is all-or-nothing ).
Reacted by Titian Cernicova-Dragomir, Daniel M., Buzurg Arjmandi, David Stemmer and Hugo Aboud
Activity
jcalz commentedon May 24, 2019
I'd be interested in more streamlined manipulation of modifiers in mapped types. Detecting
readonlyand optional properties is indeed a bunch of hoop jumping, and then if you want to selectively alter the modifiers, you need to split the mapping into pieces and intersect them:It would be a lot nicer to essentially read and selectively write modifiers inside the mapping directly. (Note the word "selectively"; the current support for altering modifiers is all-or-nothing ).
Only allow schema keys when an object is passed