打字稿:从函数定义派生接口

时间:2020-05-19 11:21:55

标签: typescript

我想做这样的事情:

function myFunction(arg1: string, arg2: number): boolean {
    //some code here
}

interface myInterface {
    someOtherProperty: string
    myFunction: myFunction // should be the equivalent of (arg1: string, arg2: Number) => boolean
}

这可能吗?

1 个答案:

答案 0 :(得分:2)

您可以在类型上下文中使用typeof来获取任何值的类型:


function myFunction(arg1: string, arg2: number): boolean {
  return false;
}

interface myInterface {
    someOtherProperty: string
    myFunction: typeof myFunction 
}

Playground Link