Home > OS >  Call Signatures example on TS Handbook
Call Signatures example on TS Handbook

Time:10-09

I am reading the Typescript Handbook and, right now, I am currently stuck at Call Signatures subsection. In the example given:

type DescribableFunction = {
  description: string;
  (someArg: number): boolean;
};

function doSomething(fn: DescribableFunction) {
  console.log(fn.description   " returned "   fn(6));
}

I cannot figure out in TS Playground how to invoke the doSomething function. I tried the below but it is not working.

doSomething({ description: "The code", (5): false})

CodePudding user response:

Functions with additional properties seem very unergonomic, at least do not know of any method of easily declaring such an instance.

In this example I use an as any first, because otherwise TS will complain about the missing description property:

const fn = ((x: number) => false) as any as DescribableFunction;
fn.description = 'description';
doSomething(fn);
  • Related