#This Does not seem to be like a Function call signature

2 messages · Page 1 of 1 (latest)

rocky hazel
#

type DescribableFunction = {
  description: string;
  (someArg: number): boolean;
};
function doSomething(fn: DescribableFunction) {
  console.log(fn.description + " returned " + fn(6));
}
 
function myFunc(someArg: number) {
  return someArg > 3;
}
myFunc.description = "default description";
 
doSomething(myFunc);

Should'nt it be like This :-

function doSomething(fn: (someArg : number) => boolean) {
  console.log(fn.description + " returned " + fn(6));
}```
compact sage
#

That will give an error at the point where you read fn.description, because most functions do not have a field called description. The (someArg: number): boolean part of DescribableFunction is called a call signature, the values of type DescribableFunction are those that

  1. have a field called description which is a string
  2. can be called, with a number, and return a boolean

So in fact DescribableFunction is a subtype of (someArg: number) => boolean.