# The this Parameter

> Typing the context of traditional functions.

Subject: TypeScript | Lesson 22 of 54 | 3 min read
Interactive lesson: https://codewithanimation.in/typescript/this-parameter
Previous: [Function Variance](https://codewithanimation.in/typescript/function-variance.md) | Next: [Basic Generics](https://codewithanimation.in/typescript/basic-generics.md)

## The fake first parameter

In JavaScript, the `this` keyword inside a traditional `function` is completely dynamic. It depends entirely on *how* the function was called.

TypeScript allows you to declare exactly what type `this` should be by using a fake, reserved first parameter named `this`.

## Preventing implicit any

If you use `this.name` inside a standalone function, TypeScript doesn't know what `this` is, so it implicitly types it as `any`. This disables type checking and can lead to runtime errors if the function is called without a proper context.

By typing the `this` parameter, you force the caller to provide the correct context.

## Syntax

```typescript
type User = { name: string };

// The 'this' parameter must be the VERY FIRST parameter.
function printName(this: User, prefix: string) {
  console.log(prefix + this.name);
}

// Error: The 'this' context of type 'void' is not assignable to type 'User'.
// printName("Hello "); 

const obj = { name: "Alice", printName };
// OK: Called as a method, so 'this' is bound to 'obj'.
obj.printName("Hello ");

// OK: Explicitly binding 'this'.
printName.call({ name: "Bob" }, "Hello ");
```

## Common mistake: It disappears at runtime

The `this` parameter is entirely a TypeScript fiction. It is completely erased during compilation.

When you call `printName("Hello ")`, you are passing `"Hello "` as the `prefix` argument, NOT as the `this` argument. The `this` parameter does not shift the position of actual runtime arguments.

## Tip: Not for Arrow Functions

You cannot use the `this` parameter on an arrow function (`() => {}`). Arrow functions permanently inherit the `this` context from their surrounding scope, so there is no dynamic context to type.

## Try it: Try it

Toggle the compile step to watch the "fake" `this` parameter evaporate from the final JavaScript code.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/this-parameter

## Check yourself

### Question 1

What happens to the 'this' parameter when the TypeScript code is compiled to JavaScript?

- A. It is converted to a standard first parameter.
- B. It is completely erased from the function signature.
- C. It is converted into a .bind() call.

Correct answer: B

The 'this' parameter is a "fake" parameter used exclusively by the TypeScript compiler to check the calling context. It disappears completely in the emitted JavaScript.

- Why not A: That would break the entire JavaScript calling convention.
- Why not C: TypeScript only erases types; it does not inject .bind() logic.

## Remember this

- To type `this`, define it as the very first parameter of the function.
- It is completely erased at runtime and doesn't affect standard arguments.
- It prevents `this` from implicitly being typed as `any`.
- It cannot be used on arrow functions.
