如何在Typescript中将属性附加到泛型类型?

问题描述 投票:0回答:1

我正在尝试将其他属性添加到泛型类型。这是我所拥有的:

type RequestData<T> = {
   name: string,
   data: {
      [K in keyof T]: T[K],
      isActive: boolean     // Typescript is not happy with this
   }
}

[基本上,我的目的是将属性isActive作为属性之一添加到T的通用类型。但是Typescript抱怨我做了什么。

我希望我的最终用法是这样的:

type TestData = { item: any[] }

const myData: RequestData<TestData> = {
   item: ['a', 'b', 'c'],
   isActive: true
}

如何将其他属性附加到泛型类型?

typescript typescript-generics
1个回答
0
投票

我不确定这是否是最好的方法,但是它可行

© www.soinside.com 2019 - 2024. All rights reserved.