1

Consider the following code from the typescript handbook call_signatures:

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

Now I want to initialise a DescribableFunction object, but I'm not sure what I should pass as the second argument. What constitutes to a valid DescribableFunction object in this situation?

Maiev
  • 43
  • 1
  • 6
  • Also relevant: [Having trouble understanding call signatures](https://stackoverflow.com/q/65113481) | [ts a type that is a function and a property at the same time](https://stackoverflow.com/q/68622766) | [Call signatures in typescript with 'this'](https://stackoverflow.com/q/68431840) | [Why does TypeScript track mutation of function static properties?](https://stackoverflow.com/q/68643123) – VLAZ Sep 15 '21 at 04:19
  • Yes, and perhaps a bit more than the comment below. Thanks. – Maiev Sep 15 '21 at 04:37

1 Answers1

1

In JavaScript (and therefore TypeScript), functions are first-class objects which can have properties and methods like any other object.

According to this definition:

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

a DescribableFunction is a function that takes a number argument and returns a boolean, and also has a description property of type string.

You can make one of these either by calling Object.assign() to copy properties into a function, like this:

const df = Object.assign(
  (someArg: number) => someArg > 3, 
  { description: "more than three" }
);
doSomething(df); // no error, logs "more than three returned true"

Or just by making a function and assigning properties to it afterward:

function df2(someArg: number) {
    return false;
}
df2.description = "always false";
doSomething(df2); // no error, logs "always false returned false"

Playground link to code

jcalz
  • 264,269
  • 27
  • 359
  • 360
  • Thanks for the explanation. Just to be sure, the DescribableFunction object is actually a function type, right? I tried to log the type of both df and df2 and got 'function', which to my understanding is that we need to initialise the object as a function, rather than a normal object like {someKey: someValue}. Am I correct? – Maiev Sep 15 '21 at 04:42
  • 1
    Yes, as far as I know you either cannot or should not try to make a normal object callable. The best way to approach it is to start with a function and add properties and not try to do the reverse. See https://stackoverflow.com/questions/19335983/can-you-make-an-object-callable for discussion. – jcalz Sep 15 '21 at 14:06