# Typescript - Tips & Tricks - typeof > Today I want to start a series of tips and tricks about Typescript. I will try to publish two/three... - **Author**: Luca Del Puppo (@puppo92) — AI Native Engineer at Nearform - **Published**: Feb 15, 2021 - **Canonical URL**: https://delpuppo.net/blog/ts-tips-and-tricks/typescript-tips-tricks-typeof/ - **Series**: TypeScript - Tips & Tricks (Part 1 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-typeof - **Dev.to Mirror**: https://dev.to/playfulprogramming/typescript-tips-tricks-typeof-nfi - **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/) **(Current Article)** - 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/) - 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/) --- Today I want to start a series of tips and tricks about Typescript. I will try to publish two/three tips & tricks a week. Before start, I want to explain why I start this series. I start this series because every day I see too many _any_ types in the typescript files and so I want to help the developers to learn better the typescript language. ## typeof Today I start with the **typeof** operator. Typeof is an operator used to detect the type of our variables. A little preamble, typeof is also a javascript operator, but with typescript, it has superpowers. Typeof in javascript has these behaviors: ```ts const valueString = "string"; const valueNumber = 1; const valueBoolean = true; const valueBigInt = 1n; const valueFunction = () => true; const valueUndefined = undefined; const valueNull = null; const object = { prop1: "value1", prop2: "value2", }; console.log(typeof valueString); // string console.log(typeof valueNumber); // number console.log(typeof valueBoolean); // boolean console.log(typeof valueBigInt); // bigint console.log(typeof valueFunction); // function console.log(typeof valueUndefined); // undefined console.log(typeof valueNull); // object console.log(typeof object); // object ``` How we can see some things are strange: e.g. when we get the typeof of a null variable javascript returns an object type. But here we see the same example in typescript: ```ts type valueStringType = typeof valueString; // "string" type valueNumberType = typeof valueNumber; // 1 type valueBooleanType = typeof valueBoolean; // true type valueBigIntType = typeof valueBigInt; // 1n type valueFunctionType = typeof valueFunction; // () => boolean type valueUndefinedType = typeof valueUndefined; // undefined type valueNullType = typeof valueNull; // null type objectType = typeof object; // { prop1: string; prop2: string; } ``` The first impact is friendlier for us developers because the typeof operator converts the type exactly as expected. The best advantages can be seen in the types: function, null, object. But now an advanced example: ```ts const item = { id: '0000', name: 'Item', unit: { id: '1', code: 'PZ' }, variants: [{ id: '1', code: '001', color: 'FFFFFF' }] }; type Item = typeof item; type Unit = typeof item["unit"]; type Variant = typeof item.variants[number]; ``` In this example, we can see how we can extract simple types from a complex object. I leave you the [in this reference](https://www.typescriptlang.org/docs/handbook/2/typeof-types.html) of the official documentation of this operator. For today is all. See you soon!