Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท tsconfig: Type Checking Options

TypeScript noImplicitThis option

Reference for the noImplicitThis compiler option: when this is an implicit any, error TS2683, this parameters, arrow functions and method extraction.

noImplicitThis makes the compiler report an error whenever the keyword this would silently get the type any. That happens inside plain functions that have no obvious owner: a standalone function expression, a callback, or a method pulled out of its object.

The option is part of the strict family, so "strict": true turns it on. You can also set it alone in compilerOptions. In JavaScript this depends on how a function is called, not on where it is written, which is exactly why the compiler wants a hint.

JSON
{
  "compilerOptions": {
    "noImplicitThis": true
  }
}

Try it

TypeScript The error and the this parameter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Methods, arrows and object literals

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

JSON
{ "compilerOptions": { "noImplicitThis": true } }
Part Description
Value true or false. Default: false, but true when strict is enabled (and strict is on by default in a project created with tsc --init).
Error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
Fix 1 Add a this parameter as the first parameter: function f(this: Counter) { ... }.
Fix 2 Use an arrow function so this comes from the enclosing scope.
Fix 3 Use a class method or an object literal method, where this is inferred.

Edge cases: the this parameter does not exist at runtime and is not counted when you call the function. ThisType<T> can set the this type of the methods in an object literal, which helps with mixin and factory helpers.

Usage notes

  • Where it fires. Only in functions whose this cannot be inferred: top-level function declarations and expressions, and functions assigned to properties with no contextual type.
  • No error in classes or literal methods. Class methods have this of the class type; methods in an object literal get the literal type (or the contextual type).
  • Top-level this. In a module, top-level this is undefined and the compiler types it that way; it is not reported.
  • Extracted methods still break at runtime. const f = account.describe; f() type-checks, because this is typed, but fails at runtime. Binding with .bind() or an arrow wrapper is the cure; strictBindCallApply checks bind, call and apply calls for you.
  • Callbacks. Passing a regular function to an API that calls it with a certain this works when the declared parameter type says so, for example (this: HTMLElement, ev: Event) => void.
  • Migration tip. Turn this option on early in a JavaScript migration; the errors are usually few and each points to real fragility.