# Typescript - Tips & Tricks - Never type > Today I want to talk about the never type. As you can understand, this type identifies a piece of... - **Author**: Luca Del Puppo (@puppo92) — AI Native Engineer at Nearform - **Published**: Mar 17, 2021 - **Canonical URL**: https://delpuppo.net/blog/ts-tips-and-tricks/typescript-tips-tricks-never-type/ - **Series**: TypeScript - Tips & Tricks (Part 14 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-never-type - **Dev.to Mirror**: https://dev.to/playfulprogramming/typescript-tips-tricks-never-type-5f0p - **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/) - 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/) **(Current Article)** - 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 talk about the [never type](https://www.typescriptlang.org/docs/handbook/2/narrowing.html#the-never-type). As you can understand, this type identifies a piece of code that will never be executed or represents a state that shouldn’t exist. Here an example ```ts const throwException: () => never = () => { throw new Error(); }; throwException() console.log('never called'); ``` In this simple example, you can see how the "throwException" method never permits the code execution of the console.log. In this case, the "throwException" method returns the _never_ type because the method doesn't return any value but throws an error. To better understand the never type I show you another simple example ```ts type Square = { kind: "square"; size: number; }; type Rectangle = { kind: "rectangle"; with: number; height: number; }; type Circle = { kind: "circle"; radius: number; }; type Shape = Square | Rectangle | Circle; const area = (shape: Shape): number => { if (shape.kind === "square") return shape.size * shape.size; if (shape.kind === "rectangle") return shape.with * shape.height; // if (shape.kind === "circle") return shape.radius * 2 * Math.PI; const _exhaustiveCheck: never = shape; // Type 'Circle' is not assignable to type 'never'. return _exhaustiveCheck; }; ``` In this example, you can see how the "area" method detects the kind of the shape and it calculates the relative area. I leave the circle type comment voluntarily, so I can show you the power of the never type. At the end of the area method, you can see how the code assigns the shape to the field "_exhaustiveCheck" of type _never_. In this case, the typescript language detects that if you pass a circle shape at the area method, the line "const _exhaustiveCheck: never = shape;" will be executed, so it notifies you raising an error. The typescript language detects that the never type could be executed at runtime so it tries to prevent this problem. To fix this problem the previous method can be reviewed in this way ```ts type Square = { kind: "square"; size: number; }; type Rectangle = { kind: "rectangle"; with: number; height: number; }; type Circle = { kind: "circle"; radius: number; }; type Shape = Square | Rectangle | Circle; const area = (shape: Shape): number => { if (shape.kind === "square") return shape.size * shape.size; if (shape.kind === "rectangle") return shape.with * shape.height; if (shape.kind === "circle") return shape.radius * 2 * Math.PI; const _exhaustiveCheck: never = shape; return _exhaustiveCheck; }; ``` Now the code is compiled successfully and the never type is assigned correctly. From the never type it's all. See you soon guys