如何在TypeScript中编写一种字符串或函数回调?

时间:2017-09-21 04:11:11

标签: typescript interface

export interface IBaseFormConfig {
    hints?: string | (Map<string, any>): string;
    validators?: any | (Map<string, any>): any;
}

这不起作用。这是一个装饰。因此开发人员可以装饰一个属性,如:

@IBaseFormConfig({
    hints: 'This is the hint'
})

// or

@IBaseFormConfig({
    hints: () => {
        // Developers can resolve its hints dynamically with services.

        return 'This is the hint';
    }
})

如何正确编写此接口属性的类型定义?

1 个答案:

答案 0 :(得分:2)

  

TypeScript中的字符串类型或函数回调?

简单:

export interface IBaseFormConfig {
  hints?: string | { (arg: Map<string, any>): string };
}

文档

它被称为可调用签名:https://basarat.gitbooks.io/typescript/content/docs/types/callable.html