Define a list of optional keys for Typescript Record

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>>

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)