我有这样的东西
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
尽管我说对象有部分。
有谁知道如何解决这个问题,以便对象可以是任意数量的这些键,并且不需要所有键?
谢谢
让你Record的Partial:
type Format = 'csv' | 'html' | 'xlsx' | 'pdf';
type Formats = Partial<Record<Format, string>>;
const formats: Formats = { csv: 'A name' };
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1136 次 |
| 最近记录: |