类型的部分记录

use*_*300 2 typescript

我有这样的东西

type Format = 'csv' | 'html' | 'xlsx' | 'pdf';
type Formats = Record<Partial<ReportFormat>, string>;
Run Code Online (Sandbox Code Playgroud)

我想创建一个基本对象,例如

{csv: 'A name'}
Run Code Online (Sandbox Code Playgroud)

但这遇到了问题TS2739: Type '{ csv: string; }' is missing the following properties from type 'ReportFormats': html, xlsx, pdf

尽管我说对象有部分。

有谁知道如何解决这个问题,以便对象可以是任意数量的这些键,并且不需要所有键?

谢谢

pza*_*ger 5

让你RecordPartial

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

const formats: Formats = { csv: 'A name' };
Run Code Online (Sandbox Code Playgroud)