The typescript documentation talks about function call signatures and construct signatures and describes how you can declare the type and use it. https://www.typescriptlang.org/docs/handbook/2/functions.html#call-signatures
call signatures (with no implemetation of a function with type DescribableFunction)
type DescribableFunction = {
description: string;
(someArg: number): boolean;
};
function doSomething(fn: DescribableFunction) {
console.log(fn.description + " returned " + fn(6));
}
construct signatures(with no implementation of a function with type SomeConstructor)
type SomeConstructor = {
new (s: string): SomeObject;
};
function fn(ctor: SomeConstructor) {
return new ctor("hello");
}
But it never shows how to define the actual implementation of such functions. And I searched a lot for days, but can't seem to find anything on it. A simple example of each with how to use it and when you would use it would be very helpful in understanding these concepts.
I tried to do it like this of call signatures but obviously its throwing error
type DescribableFunction = {
description: string;
(str: string): string;
}
const df: DescribableFunction = {
description: 'df function description',
(str: string): {
return str;
}
}
console.log(df.description, df('hello world'));