Home > other >  Typescript type throws error while having additonal property
Typescript type throws error while having additonal property

Time:10-15

I am new to typescript. While I was learning signatures, I came across this error. I declared a type with function as shown enter image description here

CodePudding user response:

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

  • Related