3

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?

2 Answers 2

5

Is this possible?

Yes as shown below:

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

function validateFormat(validator: IGrid['gridFormat']) { // MAGIC 🌹
    // ...
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.