P
P
p3trukh1n2019-06-12 17:49:14
typescript
p3trukh1n, 2019-06-12 17:49:14

What is the difference between a regular function and an arrow function in TypeScript?

Good evening.
There is an object

let user: {a: string, b: number, c: () => void} = {
    a: "exampleString",
    b: 1337,
    c() {
        console.log("exampleMethod");
    }
};

And, I noticed that if you write
let user: {a: string, b: number, c: () => void} = {
    //...

Then in the IDE prompts, the `c` method is displayed as a property,
and if you write
let user: {a: string, b: number, c(): void} = {
    //...

The way method.
I understand that this is most likely so logical, but what is the significant difference between them?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Robur, 2019-06-12
@p3trukh1n

c: () => void

this is a property whose type is a function without parameters and returning nothing. There could be any other type after :
This is just a definition of a method with the name c, without parameters and not returning anything ..
Accordingly, as you define it, ide shows you whether it is a property or a method.
There is no difference in work, but they are two different ways of defining the same thing.
There are no arrow functions in your example from the word at all.

G
grinat, 2019-06-12
@grinat

In the context, the first this is the user object, in the case of an arrow, this is the external context.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question