-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Partial<T> make only some properties optional #25760
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
Comments
You can use existing type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
type WithOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
interface Foo {
foo: string;
bar: number;
baz: boolean;
}
// { foo?: string; bar: number; baz?: boolean; }
type OptionalFoo = WithOptional<Foo, 'foo' | 'baz'>;
const createFoo = (base: OptionalFoo): Foo => {
return { foo: 'foo', baz: true, ...base };
};
const optionalFoo: OptionalFoo = { foo: '???', bar: 2 };
const fullFoo = createFoo(optionalFoo); // { foo: '???', bar: 2, baz: true } |
@0x414c interface MessageA {
mid: number;
type: number;
text: string;
}
interface MessageB {
mid: number;
type: number;
url: string;
}
type Message = MessageA | MessageB
type Keys = keyof Message // 'mid' | 'type'
/**
* {
* mid?: number;
* type: number;
* }
*/
type OptionalMessage = WithOptional<Message, 'mid'>
// what I really what :
type OptionalMessageNeed = WithOptional<MessageA, 'mid'> | WithOptional<MessageB, 'mid'> |
I just got a solution. type AllKeyOf<T> = T extends never ? never : keyof T
type Omit<T, K> = { [P in Exclude<keyof T, K>]: T[P] }
type Optional<T, K> = { [P in Extract<keyof T, K>]?: T[P] }
type WithOptional<T, K extends AllKeyOf<T>> = T extends never ? never : Omit<T, K> & Optional<T, K>
/**
{
mid?: number;
type: number;
text?: string;
}
| {
mid?: number;
type: number;
url: string;
}
*/
type a = WithOptional<Message, 'mid' | 'text'> |
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. |
This works for me type Optional<T, K extends keyof T> = Omit<T, K> & Partial<T>; |
This is really good. However, is there a way to keep the keys ordered as the original type? |
|
@rattrayalex Thank you! This has all that I could need and more |
|
Edited on 2023-09-06: In case you need to ensurethat // All props are required but some are optional
type Optional<T, K extends keyof T> = Omit<T, K> & Partial<T>;
type Test = {
foo: string;
bar: string;
baz: string;
daz?: string;
};
type Test1 = Optional<Test, "foo">;
const test1: Test1 = {
bar: '',
baz: ''
}
type Test2 = Optional<Test, "bar" | "baz">;
const test2: Test2 = {
foo: ''
}
// All prop are optional but some of those are required
type PartialWithRequired<T, K extends keyof T> = Pick<T, K> & Partial<T>;
type Test3 = PartialWithRequired<Test, 'bar' | 'foo'>;
const test3:Test3 = {
bar: '',
foo: ''
} |
@tresorama thanks for that starting point. It seems that since type 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: type Test = {
foo?: string;
bar?: string;
baz: string;
};
// All prop are optional but some of those are required -- NOTE! doesn't work, see Test1.
type PartialWithRequired<T, K extends keyof T> = Pick<T, K> & Partial<T>;
type Test1 = PartialWithRequired<Test, 'foo'|'bar'>;
// @ts-expect-error since `Pick` from `PartialWithRequired` does NOT mark an optional parameter like 'foo' or 'bar' as required
const test1:Test1 = {}
// Actually works:
type PartialExceptTheseRequired<T, K extends keyof T> = Pick<Required<T>, K> & Partial<T>
type Test2 = PartialExceptTheseRequired<Test, 'foo'|'bar'>
const test2:Test2 = {}
// ^ it errors, which we want, since we are now requiring foo and bar but haven't set them. |
@techieshark Fantastic! |
Search Terms
Suggestion
Use Cases
current
Partial<T>
will make all properties ofT
optionalI want to specify only some properties of
T
optionalExamples
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 aboutComplement
?Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: