从界面创建记录,类型为 return,类型为原始界面的 属性

Create record from interface with return type as property type from original interface

我有一个像

这样的界面
inteface A {
  a: number
}

我想创建一个对象类型,其中键作为 A 的键,值作为函数,return 类型的键

例子

const obj = {
  a: () => 4
}

这里 obj 的类型应该是什么,它应该是采用 A 接口的泛型

您可以为此使用映射类型:

type Functionify<T> = { [K in keyof T]: () => T[K] }

它遍历类型 T 中的所有键(属性)并将它们映射到返回原始类型 T[K] 的函数。

Playground