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

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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 TS is able in this case to track mutations. See my question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.