Michael.Lumley
Michael.Lumley

Reputation: 2385

Typescript: Derive Interface from Function Definition

I'd love to do something like this:

function myFunction(arg1: string, arg2: number): boolean {
    //some code here
}

interface myInterface {
    someOtherProperty: string
    myFunction: myFunction // should be the equivalent of (arg1: string, arg2: Number) => boolean
}

Is this possible?

Upvotes: 1

Views: 24

Answers (1)

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 249466

You can use typeof in a type context to get the type of any value:


function myFunction(arg1: string, arg2: number): boolean {
  return false;
}

interface myInterface {
    someOtherProperty: string
    myFunction: typeof myFunction 
}

Playground Link

Upvotes: 2

Related Questions