键入对象文字的速记方法

时间:2018-11-30 15:43:14

标签: typescript

如果我有一个异步函数类型定义,例如

type MyAsyncFunctionType = (arg:string) => Promise<string>

如何使用ES2015 object method shortand syntax来使用它在对象常量上键入我定义的异步函数?

const myObject = {
  async myFunction(arg) {
    /* ... */
  }
}

1 个答案:

答案 0 :(得分:2)

有点麻烦,您可能需要将其提取为命名类型,但可以这样输入:

type MyAsyncFunctionType = (arg: string) => Promise<string>

const myObject: { myFunction: MyAsyncFunctionType } = {
    async myFunction(arg) {
        // ... code here
    }
}
相关问题