@@ -289,15 +289,14 @@ It will point out that `this` in `this.suits[pickedSuit]` is of type `any`.
289289
290290## ` this ` parameters
291291
292- Unfortunately, you will find that TypeScript still says ` this ` in ` this.suits[pickedSuit] ` is of type ` any ` even if you use the arrow function .
292+ Unfortunately, by default TypeScript will still type ` this ` in ` this.suits[pickedSuit] ` as ` any ` .
293293That's because TypeScript doesn't know the type of ` this ` inside a function expression.
294294To fix this, you can provide an explicit ` this ` parameter.
295295` this ` parameters are fake parameters that come first in the parameter list of a function:
296296
297297``` ts
298298function f(this : void ) {
299- // in strict mode, `this` is undefined for a standalone function,
300- // represented by the void type here
299+ // make sure `this` is unusable in this standalone function
301300}
302301```
303302
@@ -340,7 +339,7 @@ That means that `this` is of type `Deck` now, not `any`, so `--noImplicitThis` w
340339
341340You can also run into errors with ` this ` in callbacks, when you pass functions to a library that will later call them.
342341Because the library that calls your callback will call it like a normal function, ` this ` will be ` undefined ` .
343- With some work you can use ` this ` to prevent you from making errors in callbacks too.
342+ With some work you can use ` this ` parameters to prevent errors with callbacks too.
344343First, the library author needs to annotate the callback type with ` this ` :
345344
346345``` ts
@@ -350,25 +349,51 @@ interface UIElement {
350349```
351350
352351` this: void ` means that ` addClickListener ` expects ` onclick ` to be a function that does not require a ` this ` type.
353- Second, annotate your code with ` this ` :
352+ Second, annotate your calling code with ` this ` :
354353
355354``` ts
356- class Handlers {
357- static onClickBad(this : typeof Handlers , e : Event ) {
355+ class Handler {
356+ info: string ;
357+ onClickBad(this : Handler , e : Event ) {
358358 // oops, used this here. using this callback would crash at runtime
359- this .onClickGood ( e ) ;
359+ this .info = e . message ;
360360 };
361- static onClickGood(this : void , e : Event ) {
361+ }
362+ let h = new Handler ();
363+ uiElement .addClickListener (h .onClickBad ); // error!
364+ ```
365+
366+ With ` this ` annotated, you make it explicit that ` onClickBad ` must be called on an instance of ` Handler ` .
367+ Then TypeScript will detect that ` addClickListener ` requires a function that has ` this: void ` .
368+ To fix the error, change the type of ` this ` :
369+
370+ ``` ts
371+ class Handler {
372+ info: string ;
373+ onClickGood(this : void , e : Event ) {
362374 // can't use this here because it's of type void!
363375 console .log (' clicked!' );
364376 }
365377}
366- uiElement .addClickListener (Handlers .onClickBad );
367- uiElement .addClickListener (Handlers .onClickGood );
378+ let h = new Handler ();
379+ uiElement .addClickListener (h .onClickGood );
380+ ```
381+
382+ Because ` onClickGood ` specifies its ` this ` type as ` void ` , it is legal to pass to ` addClickListener ` .
383+ Of course, this also means that it can't use ` this.info ` .
384+ If you want both then you'll have to use an arrow function:
385+
386+ ``` ts
387+ class Handler {
388+ info: string ;
389+ onClickGood = (e : Event ) => { this .info = e .message }
390+ }
368391```
369392
370- Now you will get an error when you try to register ` onClickBad ` because its ` this ` is ` typeof Handlers ` , which ` addClickListener ` does not accept.
371- In contrast, because ` onClickGood ` specifies its ` this ` type as ` void ` , it is legal to pass to ` addClickListener ` .
393+ This works because arrow functions don't capture ` this ` , so you can always pass them to something that expects ` this: void ` .
394+ The downside is that one arrow function is created per object of type Handler.
395+ Methods, on the other hand, are only created once and attached to Handler's prototype.
396+ They are shared between all objects of type Handler.
372397
373398# Overloads
374399
0 commit comments