user3623300
user3623300

Reputation: 379

Partial record from type

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

Upvotes: 2

Views: 1512

Answers (1)

pzaenger
pzaenger

Reputation: 11973

Make your Record a Partial:

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

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

Upvotes: 5

Related Questions