首页 > 解决方案 > 打字稿对象声明,其中每个道具都有自己的泛型

问题描述

我正在尝试创建更动态的对象类型。现在我有以下内容:

interface MyGeneric<T> {
  value: T
}

interface MyObject {
  [name: string]: MyGeneric<any>;
}

但我想把它抽象成这样:

interface MyGeneric<T> {
  value: T
}

interface MyObject {
  [name: string]<T>: MyGeneric<T>;
}

这种语法不起作用,但我想要的是允许对象上的每个道具都有自己的泛型类型。

关于这可能是什么样子的任何建议?

标签: typescript

解决方案


推荐阅读