# Typescript - Tips & Tricks - this parameter > Hello everybody and welcome back, today I'll talk about this parameter. Sometimes we need to create... - **Author**: Luca Del Puppo (@puppo92) — AI Native Engineer at Nearform - **Published**: Feb 26, 2021 - **Canonical URL**: https://delpuppo.net/blog/ts-tips-and-tricks/typescript-tips-tricks-this-parameter/ - **Series**: TypeScript - Tips & Tricks (Part 6 of 21) — https://delpuppo.net/blog/series/ts-tips-and-tricks/ - **Series LLMs.txt**: https://delpuppo.net/blog/series/ts-tips-and-tricks/llms.txt - **Category**: TypeScript - **Tags**: typescript, webdev - **Hashnode Mirror**: https://blog.delpuppo.net/typescript-tips-tricks-this-parameter - **Dev.to Mirror**: https://dev.to/playfulprogramming/typescript-tips-tricks-this-parameter-7n6 - **Blog Index LLMs.txt**: https://delpuppo.net/blog/llms.txt ## Series Context: TypeScript - Tips & Tricks - Part 1: [Typescript - Tips & Tricks - typeof](https://delpuppo.net/blog/ts-tips-and-tricks/typescript-tips-tricks-typeof/) - Part 2: [Typescript - Tips & Tricks - Literal Types](https://delpuppo.net/blog/ts-tips-and-tricks/typescript-tips-tricks-literal-types/) - Part 3: [Typescript - Tips & Tricks - keyof](https://delpuppo.net/blog/ts-tips-and-tricks/typescript-tips-tricks-keyof/) - Part 4: [Typescript - Tips & Tricks - Union and Intersection](https://delpuppo.net/blog/ts-tips-and-tricks/typescript-tips-tricks-union-and-intersection/) - Part 5: [Typescript - Tips & Tricks - Overloaded Functions](https://delpuppo.net/blog/ts-tips-and-tricks/typescript-tips-tricks-overloaded-functions/) - Part 6: [Typescript - Tips & Tricks - this parameter](https://delpuppo.net/blog/ts-tips-and-tricks/typescript-tips-tricks-this-parameter/) **(Current Article)** - Part 7: [Typescript - Tips & Tricks - any vs. unknown](https://delpuppo.net/blog/ts-tips-and-tricks/typescript-tips-tricks-any-vs-unknown/) - Part 8: [Typescript - Tips & Tricks - Optional modifier](https://delpuppo.net/blog/ts-tips-and-tricks/typescript-tips-tricks-optional-modifier/) - Part 9: [Typescript - Tips & Tricks - Index Signature](https://delpuppo.net/blog/ts-tips-and-tricks/typescript-tips-tricks-index-signature/) - Part 10: [Typescript - Tips & Tricks - readonly modifier](https://delpuppo.net/blog/ts-tips-and-tricks/typescript-tips-tricks-readonly-modifier/) - Part 11: [Typescript - Tips & Tricks - Readonly Array](https://delpuppo.net/blog/ts-tips-and-tricks/typescript-tips-tricks-readonly-array/) - Part 12: [Typescript - Tips & Tricks - Tuple](https://delpuppo.net/blog/ts-tips-and-tricks/typescript-tips-tricks-tuple/) - Part 13: [Typescript - Tips & Tricks - Non-null assertion operator](https://delpuppo.net/blog/ts-tips-and-tricks/typescript-tips-tricks-non-null-assertion-operator/) - Part 14: [Typescript - Tips & Tricks - Never type](https://delpuppo.net/blog/ts-tips-and-tricks/typescript-tips-tricks-never-type/) - Part 15: [Typescript - Tips & Tricks - Assert Function](https://delpuppo.net/blog/ts-tips-and-tricks/typescript-tips-tricks-assert-function/) - Part 16: [Typescript - Tips & Tricks - Type Guard](https://delpuppo.net/blog/ts-tips-and-tricks/typescript-tips-tricks-type-guard/) - Part 17: [Typescript - Tips & Tricks - Conditional Type](https://delpuppo.net/blog/ts-tips-and-tricks/typescript-tips-tricks-conditional-type/) - Part 18: [Typescript - Tips & Tricks - infer keyword](https://delpuppo.net/blog/ts-tips-and-tricks/typescript-tips-tricks-infer-keyword/) - Part 19: [Typescript - Tips & Tricks - Mapped Types](https://delpuppo.net/blog/ts-tips-and-tricks/typescript-tips-tricks-mapped-types/) - Part 20: [Typescript - Tips & Tricks - Advanced Types](https://delpuppo.net/blog/ts-tips-and-tricks/typescript-tips-tricks-advanced-types/) - Part 21: [Typescript - Tips & Tricks - Rest Parameters & Rest Arguments](https://delpuppo.net/blog/ts-tips-and-tricks/typescript-tips-tricks-rest-parameters-rest-arguments/) --- Hello everybody and welcome back, today I'll talk about [this parameter](https://www.typescriptlang.org/docs/handbook/functions.html#this-parameters). 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: ```ts 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: ```ts 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: ```ts 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!