A
A
Alexander2020-01-27 20:07:31
typescript
Alexander, 2020-01-27 20:07:31

How to use this in an arrow function in typescript?

I'm trying to use this in an arrow function, after specifying the type for this.
I get an error: An arrow function cannot have a 'this' parameter.ts(2730)
5e2f1774e70d9070822112.png

According to this PR ( https://github.com/Microsoft/TypeScript/pull/6739 ) in typescript you can use this in arrow functions in this way. There are absolutely no hints on the Internet to explain the TS2730 error. In general, I'm confused.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2020-01-27
@LordGuard

Judging by this PR in typescript, you can use this in arrow functions

uh .. maybe I'm looking in the wrong place, but it says just that it's pointless. And there is not a single example of typing this in arrows.
This formulation is OK because lambda (=>) doesn't bind this, so this comes from the class instead of from the implementing function.

In general, you can first declare the type for the function, like this
https://www.typescriptlang.org/play/?noImplicitThi...
But it also works crookedly.
Why did you even need it?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question