Typescript - Tips & Tricks - this parameter

Full Stack Developer and JavaScript and TypeScript enthusiastic.
Search for a command to run...

Full Stack Developer and JavaScript and TypeScript enthusiastic.
No comments yet. Be the first to comment.
In this series, You'll see some tips and tricks using Typescript
Hi guys, today I'll talk about the difference between any and unknown. Any Any type is a particular type in typescript. If you use this type, the compiler doesn't check your code, and the power is in your hands, but from great powers comes great resp...
Today, I wanna show you how to set up your devices with Antigravity using only the agentic mode, without touching any code. At the last CodeMotion Rome, I got my special badge. A CM Node, a special ha

As a developer, you have to take control of your projects every day. Whether it is a company repository, an open source project you maintain or collaborate on, or a simple pet project. Gaining control over your projects often depends on the platform ...

My Antigravity first experience

Write clear and insightful commit messages using AI and GitLens.

We are in the AI era. New models emerge daily, and many applications have already integrated AI into their workflows. Gemini, OpenAI, Copilot, Deepseek, Llama, and many others enable AI in your applications or help you write code, but they all need a...

Hello everybody and welcome back, today I'll talk about this parameter.
Sometimes we need to create functions that have to know the context to run correctly. A simple example, the mathematical power must know the base and the exponent number to run correctly. So we could create a simple method like this:
function pow(exponent: number) {
return Math.pow(this.number, exponent);
}
const basePow = {
base: 2,
pow,
};
console.log(basePow.pow(2));
If we build and run this code, the console log's result will be "NaN", because in the pow method the value of the field "this.number" is undefined. N.B. if you build this code with the strict mode on true, the compiler detects the problem at build time and so the build fails
But now I explain the solution. In this case, the problem is the "number" field that doesn't exist in this context. Typescript helps us to prevent this error, it allows us to indicate the "this" type as a parameter. To do this, we need to declare the "this" context as the first parameter of the function, so typescript detects the "this" type and it helps us to prevent errors and bugs. So the code in the previous example should be rewritten like this:
function pow(this: { base: number }, exponent: number) {
return Math.pow(this.base, exponent);
}
const basePow = {
base: 2,
pow,
};
console.log(basePow.pow(2));
In this case, the result at runtime is "4" as expected. If by chance we write the "basePow" object like this:
const basePow = {
value: 2,
pow,
};
when we try to call the method "basePow.pow(2)", the typescript language detects the error, and in this case, when we try to build the code the build fails.
That's all! See you soon guys!