el_pup_le
el_pup_le

Reputation: 12179

Function parameter to use interface's function signature

I have a function signature in an interface which I'd like to use as the signature for a callback parameter in some class.

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

I'd like to do something like this:

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

Is this possible?

Upvotes: 3

Views: 711

Answers (2)

Sayan Pal
Sayan Pal

Reputation: 4946

You may try something like the following:

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

declare let igrid: IGrid;

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

Additionally, you may also declare a type for the method like below

declare type gridFormatMethodType = typeof igrid.gridFormat

to avoid the cumbersome method signature for validateFormat

validateFormat(validator: gridFormatMethodType){ ... }

Hope this helps.

Upvotes: 1

basarat
basarat

Reputation: 276199

Is this possible?

Yes as shown below:

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

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

Upvotes: 5

Related Questions