Define a list of optional keys for Typescript Record
You can create the partial version of your List
type:
type PartialList = Partial<List>;
And you could do it all on one line if you don't want the intermediate type:
type PartialList = Partial<Record<'a' | 'b' | 'c', string>>;
You might decide that, in the end, the most expressive for your future self is:
type List = {
a?: string;
b?: string;
c?: string;
}
Looks like in new versions of typescript you may do the following
type YourUnion = 'a' | 'b' | 'c';
type ObjectWithOptionalKeys = Partial<Record<YourUnion, string>>
const someObject: ObjectWithOptionalKeys {
a: 'str', // works
b: 1 // throws
}
// c may not be specified at all
There is no way to specify the optionality of members of Record
. They are required by definition
type Record<K extends keyof any, T> = {
[P in K]: T; // Mapped properties are not optional, and it's not a homomorphic mapped type so it can't come from anywhere else.
};
You can define your own type if this is a common scenario for you:
type PartialRecord<K extends keyof any, T> = {
[P in K]?: T;
};
type List = PartialRecord<'a' | 'b' | 'c', string>
Or you can define PartialRecord
using the predefined mapped types as well:
type PartialRecord<K extends keyof any, T> = Partial<Record<K, T>>