Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial record from type [duplicate]

Tags:

typescript

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

like image 392
user3623300 Avatar asked Feb 01 '26 21:02

user3623300


1 Answers

Make your Record a Partial:

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

const formats: Formats = { csv: 'A name' };
like image 84
pzaenger Avatar answered Feb 04 '26 14:02

pzaenger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!