Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is TypeScript's ThisType used for?

Tags:

typescript

TypeScript comes with an interface ThisType. I couldn't find much documentation on it, so I'm wondering: what is it used for? How do I use it? How does it work?

like image 286
feihcsim Avatar asked Mar 06 '19 17:03

feihcsim


People also ask

Why we use this in TypeScript?

One of the most important concepts to grasp in TypeScript is the use of the "this" keyword, which is used in object methods. The "this" keyword always points to the object that is calling a particular method.

What is TypeScript and where it is used?

TypeScript is a free and open source programming language developed and maintained by Microsoft. It is a strict syntactical superset of JavaScript and adds optional static typing to the language. It is designed for the development of large applications and transpiles to JavaScript.

What is ?: In TypeScript?

What does ?: mean in TypeScript? Using a question mark followed by a colon ( ?: ) means a property is optional. That said, a property can either have a value based on the type defined or its value can be undefined .

When should TypeScript be used?

Developers using JavaScript from other languages often complain about the lack of strong static characters, but that's where TypeScript comes in to fill that gap. TypeScript is a superset of typed JavaScript (optional) that can help build and manage large-scale JavaScript projects.


1 Answers

ThisType is documented here: https://www.typescriptlang.org/docs/handbook/utility-types.html#thistypet (thanks to @axiac for linking that in the comments)

However, the example the docs use feels a bit convoluted to me. This is the easiest way I've found to think about it:

If you add & ThisType<WhateverYouWantThisToBe> to the type of an object, the functions within that object will have WhateverYouWantThisToBe as the type used for this. This is helpful if you can't otherwise specify what this should be through the normal TypeScript mechanisms (it's not a class, etc.)

If we imagine a world where we're writing some code that registers some helper functions, you could write it like this:

interface HelperThisValue {
    logError: (error: string) => void;
}

let helperFunctions: { [name: string]: Function } & ThisType<HelperThisValue> = {
    hello: function() {
        this.logError("Error: Something went wrong!"); // TypeScript successfully recognizes that "logError" is a part of "this".
        this.update(); // TS2339: Property 'update' does not exist on HelperThisValue.
    }
}

registerHelpers(helperFunctions);

As I've indicated in the comments above, TypeScript now knows the correct type, and will give errors if we misuse it.

Note that like all of TypeScript's typings, using ThisType has no effect on the resulting JavaScript code. There must be code elsewhere that ensures that when the members of helperFunctions get called, the proper this value is set (e.g. through use of Function.bind, Function.apply, Function.call, etc.)

like image 191
Tyler Church Avatar answered Sep 18 '22 18:09

Tyler Church