2

Currently, I am learning TypeScript. I am quite confused about the difference between keyword function and => (fat arrow). Please see the code below:

interface Counter {
    (start: number);
    interval: number;
    reset() : void;
}

let a = <Counter>function(start: number) { };
let b = <Counter>(start: number) => { };

a.reset(); //OK
b.reset(); //error: Property 'reset' does not exist on type <Counter>(start: number) => void

It seems that fat arrow doesn't work the same as keyword function.

2
  • This has nothing to do with TypeScript. It is purely a JS (ES6) issue. The difference is well explained in hundreds of blog posts, documentation pages, and questions right here on SO. Commented Dec 13, 2016 at 7:31
  • @torazaburo Got it. Thanks a lot. Commented Dec 13, 2016 at 7:43

2 Answers 2

3

fat arrow functions have a shorter syntax compared to function expressions and lexically bind the this value. Arrow functions are always anonymous and effectively turn function (arguments) { return expression; } into (arguments) => expression. If using an expression after an arrow, the return is implicit, so no return is required.

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

1 Comment

The term "fat arrow function" is obsolete. The correct term is "arrow function".
2

It's all about keeping the context/scope in JavaScript. Take a look here: What is this in JavaScript and Typescript.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.