Home > Blockchain >  Partial record from type
Partial record from type

Time:05-12

I've got something like this

type Format = 'csv' | 'html' | 'xlsx' | 'pdf';
type Formats = Record<Partial<ReportFormat>, string>;

and I would like to create a basic object like

{csv: 'A name'}

But this runs into the problem TS2739: Type '{ csv: string; }' is missing the following properties from type 'ReportFormats': html, xlsx, pdf

Even though I said the object had partial.

Does anyone have an idea how to fix this so that the object can be any number of those keys, and so that not all are required?

Thank you

CodePudding user response:

Make your Record a Partial:

type Format = 'csv' | 'html' | 'xlsx' | 'pdf';
type Formats = Partial<Record<Format, string>>;

const formats: Formats = { csv: 'A name' };
  • Related