3
interface DescribableFunction  {
  description: string;
  (f: number,s:number): number;
};

function doSomething(fn: DescribableFunction) {
  console.log( " returned " + fn(3,4));
}

//here,"const" is right,"let" will be error
const p:DescribableFunction=(first:number,second:number)=>first+second;
p.description="hello";

doSomething(p);

if DescribableFunction has no "description" field,const or let will be ok I test it at https://www.typescriptlang.org/play

uzaidabin
  • 31
  • 1

1 Answers1

3

Here you have a documentation:

TypeScript 3.1 brings the ability to define properties on function declarations and const-declared functions, simply by assigning to properties on these functions in the same scope. This allows us to write canonical JavaScript code without resorting to namespace hacks. For example:

function readImage(path: string, callback: (err: any, image: Image) => void) {
  // ...
}
readImage.sync = (path: string) => {
  const contents = fs.readFileSync(path);
  return decodeImageSync(contents);
};

So, this is design decision.

  • 1
    Thanks,I found const function is special,```const mutable = (a:string)=>console.log(a); mutable.b = 2;``` is right,but ```const mutable ={a:1}; mutable.b = 2;``` has error for this reason. – uzaidabin Jan 26 '22 at 14:49
  • @uzaidabin TS is able in this case to track mutations. See my [question](https://stackoverflow.com/questions/68643123/why-does-typescript-track-mutation-of-function-static-properties) – captain-yossarian from Ukraine Jan 26 '22 at 14:55