【发布时间】:2022-06-28 23:37:02
【问题描述】:
我需要描述一个接口,其中:
- 具有“billingAddress”键的属性的值是具有特定属性的对象,并且
- 具有任何其他键的属性具有字符串值。
我试过了:
interface DoesNotWork {
[key: string]: string;
billingAddress?: {
foo: string;
}
}
Typescript 抱怨 Property 'billingAddress' of type '{ foo: string; } | undefined' is not assignable to 'string' index type
很公平:当 DoesNotWork.billingAddress 被定义时,Typescript 将不知道是否应该为它分配 string、object 或 undefined。
如何以 Typescript 可以理解的方式描述界面?
【问题讨论】:
标签: typescript