【发布时间】:2021-12-24 12:57:41
【问题描述】:
如何为对象创建 typescript 类型,我可以在其中拥有一组固定键和动态键集?例如:
export type APIOMethods = 'getA' | 'getB' | 'getC';
export type item = {
divId: string;
role: string;
[key: K in APIMethods]?: any;
};
// Not working ↑↑↑ ????
export type item = {
divId: string;
role: string;
[key: APIMethods]: any;
};
// Not working ↑↑↑ ????
// the object could look like this:
{divId: '', role: '', getA: {}} OR
{divId: '', role: '', getB: {}} OR
{divId: '', role: '', getC: {}}
- Typescript Playground 链接here
【问题讨论】:
标签: javascript typescript typescript-typings typescript-generics