1

I am new to typescript. While I was learning signatures, I came across this error. I declared a type with function as shown here. It was working fine when I had only the function, later I introduced an additional property to that type, and I don't how to use the type after that.

type GreetFunction = {
  (x: string): void;
};
function greeter(fn: GreetFunction) {
 fn("Hello");
}

function printToConsole(a: string) {
  console.log(a);
}

greeter(printToConsole);  //This is working fine ( Converting to js file )

But the below code is not converting and giving errors.

type GreetFunction = {
  desc: string;
  (x: string): void;
};
function greeter(fn: GreetFunction) {
  fn("Hello");
}

function printToConsole(a: string) {
  console.log(a);
}

greeter(printToConsole); //This is line is giving error

enter image description here

Badhusha
  • 195
  • 2
  • 20

1 Answers1

0

You need to mutate printToConsole and add desc property.

type GreetFunction = {
  desc: string;
  (x: string): void;
};
function greeter(fn: GreetFunction) {
  fn("Hello");
}

function printToConsole(a: string) {
  console.log(a);
}
printToConsole.desc = 'df' // <--------------- add desc property

greeter(printToConsole); // ok

Playground

This is the rare case when TypeScript tracks mutations. Please see similar question