是否可以使嵌套对象属性具有泛型类型?
大概是这样的
type func = <P extends any>(props: P) => ...
type Obj = {
[key: string]: {f: func, args: PARAMETERS OF "f"}
}
const demo: Obj = {
// here the type of "args" should be {name: string, age: number}
key1: {f: (props: {name: string, age: number}) => ..., args: HERE SHOULD BE TYPED},
// here the type of "args" should be {somethingRandom: number[]}
key2: {f: (props: {somethingRandom: number[]}) => ..., args: HERE SHOULD BE TYPED},
... more properties
}
1条答案
按热度按时间zu0ti5jz1#
您可能正在搜索
satisfies
:打印稿文档:
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-9.html#the-satisfies-operator
(note您需要TypeScript 4.9+来完成此操作)