2017-06-19 111 views
3

我在接口中有一个函数签名,我想用它作为某些类中的回调参数的签名。使用接口函数签名的函数参数

export interface IGrid { 
    gridFormat(gridCell: GridCell, grid: Grid): boolean 
} 

我想要做这样的事情:

validateFormat(validator: IGrid.gridFormat) { 
    // ... 
} 

这可能吗?

回答

4

这可能吗?如下所示

是:

export interface IGrid { 
    gridFormat(gridCell: GridCell, grid: Grid): boolean 
} 

function validateFormat(validator: IGrid['gridFormat']) { // MAGIC 
    // ... 
} 
1

您可以尝试类似以下内容:

export interface IGrid { 
    gridFormat(gridCell: GridCell, grid: Grid): boolean 
} 

declare let igrid: IGrid; 

export class Test { 
    validateFormat(validator: typeof igrid.gridFormat) { 
    // ... 
    } 
} 

此外,您还可以声明式的方法,如下面

declare type gridFormatMethodType = typeof igrid.gridFormat 

避免了繁琐的方法签名validateFormat

validateFormat(validator: gridFormatMethodType){ ... } 

希望这会有所帮助。