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
Partial<T> make only some properties optional #25760
it's very often sometimes we want to omit some properties of an object and later supplement it.
currently typescript has concepts like Intersection、Union, how about Complement?
Checklist
My suggestion meets these guidelines:
This wouldn't be a breaking change in existing TypeScript / JavaScript code
This wouldn't change the runtime behavior of existing JavaScript code
This could be implemented without emitting different JS based on the types of the expressions
This isn't a runtime feature (e.g. new expression-level syntax)
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
Guys those solutions are unredable. I think it's better to create separate interface.
Reacted by SnorreDanielsen, Bernhard Häussner, Filipe Dos Santos Mendes, Daniel, Brooke Holmes, Adam Gerthel, Robert Konigsberg, Huy, Diego Mendoza Pinto, Jeremy and 9 more
Reacted by Bjørnar G. Hveding, CyberCookie, 小黄黄, Santiago "Momo" Marín, riccardo-felipe, Caleb Collins-Parks, Michael Dovzhenko, Vahid Hallaji, Jan Koriťák, Pier-Luc Gendreau and 48 more
Reacted by Santiago "Momo" Marín, Jan Koriťák, Ali Zain Ul Yasoob, Stark, Sergey Malchits, Maxim Golovanev, Vadim, PaulKujawa, Ronny, Pedro Palhari and 1 more
Edited on 2023-09-06:
The PartialWithRequired present here will not convert property declared as optional (like daz?: string) as required. In case is what you need use the great code of Peter W (@techieshark) of this comment
In case you need to ensurethat All prop are optional but some of those are required this worked for me (Thanks to Gregor Martynus (@gr2m) for the starting point.)
// All props are required but some are optionaltypeOptional<T,KextendskeyofT>=Omit<T,K>&Partial<T>;typeTest={foo: string;bar: string;baz: string;daz?: string;};typeTest1=Optional<Test,"foo">;consttest1: Test1={bar: '',baz: ''}typeTest2=Optional<Test,"bar"|"baz">;consttest2: Test2={foo: ''}// All prop are optional but some of those are requiredtypePartialWithRequired<T,KextendskeyofT>=Pick<T,K>&Partial<T>;typeTest3=PartialWithRequired<Test,'bar'|'foo'>;consttest3:Test3={bar: '',foo: ''}
Jacopo Marrone (@tresorama) thanks for that starting point. It seems that since type Test already requires all properties but one (daz?), the code provided works.
But if starting out with optional properties and we want to actually make them required, we can also use the Required<> utility. Here's this:
typeTest={foo?: string;bar?: string;baz: string;};// All prop are optional but some of those are required -- NOTE! doesn't work, see Test1.typePartialWithRequired<T,KextendskeyofT>=Pick<T,K>&Partial<T>;typeTest1=PartialWithRequired<Test,'foo'|'bar'>;// @ts-expect-error since `Pick` from `PartialWithRequired` does NOT mark an optional parameter like 'foo' or 'bar' as requiredconsttest1:Test1={}// Actually works:typePartialExceptTheseRequired<T,KextendskeyofT>=Pick<Required<T>,K>&Partial<T>typeTest2=PartialExceptTheseRequired<Test,'foo'|'bar'>consttest2:Test2={}// ^ it errors, which we want, since we are now requiring foo and bar but haven't set them.
Activity
0x414c commentedon Jul 18, 2018
You can use existing
PartialandExcludeto implement an approach like this:typescript-bot commentedon Jul 30, 2018
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
kacluk123 commentedon Mar 16, 2020
Guys those solutions are unredable. I think it's better to create separate interface.
gr2m commentedon Apr 16, 2020
This works for me
Playground link
ftzi commentedon Oct 7, 2020
This is really good. However, is there a way to keep the keys ordered as the original type?
rattrayalex commentedon Jul 27, 2021
type-festhasSetOptional, for those who would rathernpm install --devutilities like this.b-steel commentedon Aug 26, 2021
Alex Rattray (@rattrayalex) Thank you! This has all that I could need and more
mfuatnuroglu commentedon Aug 27, 2021
type onlyOneRequired = Partial<Tag> & {id: string}In the above type all properties of Tag are optional instead
idwhich is required.tresorama commentedon Sep 19, 2022
Edited on 2023-09-06:
The
PartialWithRequiredpresent here will not convert property declared as optional (likedaz?: string) as required. In case is what you need use the great code of Peter W (@techieshark) of this commentIn case you need to ensurethat
All prop are optional but some of those are requiredthis worked for me (Thanks to Gregor Martynus (@gr2m) for the starting point.)Playground
techieshark commentedon Sep 6, 2023
Jacopo Marrone (@tresorama) thanks for that starting point. It seems that since type
Testalready requires all properties but one (daz?), the code provided works.But if starting out with optional properties and we want to actually make them required, we can also use the Required<> utility. Here's this:
Playground
tresorama commentedon Sep 6, 2023
Peter W (@techieshark) Fantastic!
I didn't know
Required