As I continued my journey through the TypeScript handbook, I stumbled upon an intriguing concept while learning about Call Signatures. The code snippet provided in the handbook goes like this:
type DescribableFunction = {
description: string;
(someArg: number): boolean;
};
function doSomething(fn: DescribableFunction) {
console.log(fn.description + " returned " + fn(6));
}
I decided to experiment by creating a function that could be executed by doSomething. However, I encountered a perplexing issue when trying to define the function as a named expression using let
. It gave me an error stating
"Property 'description' does not exist on type '(n: number) => boolean'.(2339)"
while assigning the description property. Strangely, defining the function as a function declaration or with const
allowed me to assign the property without any issues... Why is that?
Take a look at the example below for clarification.
type DescribableFunction = {
description: string;
(someArg: number): boolean;
};
function doSomething(fn: DescribableFunction) {
console.log(fn.description + " returned " + fn(6));
}
function f(n:number){
return true;
}
f.description = "bla bla bla";
let testFunc = function f(n: number) {
return true;
}
testFunc.description = "bla bla bla"; //This shows an error!!
const testFunc2 = function f(n: number) {
return true;
}
testFunc2.description = "bla bla bla";