
- TypeScript - Home
- TypeScript - Roadmap
- TypeScript - Overview
- TypeScript - Environment Setup
- TypeScript - Basic Syntax
- TypeScript vs. JavaScript
- TypeScript - Features
- TypeScript - Variables
- TypeScript - let & const
- TypeScript - Operators
- TypeScript - Types
- TypeScript - Type Annotations
- TypeScript - Type Inference
- TypeScript - Numbers
- TypeScript - Strings
- TypeScript - Boolean
- TypeScript - Arrays
- TypeScript - Tuples
- TypeScript - Enums
- TypeScript - Any
- TypeScript - Never
- TypeScript - Union
- TypeScript - Literal Types
- TypeScript - Symbols
- TypeScript - null vs. undefined
- TypeScript - Type Aliases
- TypeScript Control Flow
- TypeScript - Decision Making
- TypeScript - If Statement
- TypeScript - If Else Statement
- TypeScript - Nested If Statements
- TypeScript - Switch Statement
- TypeScript - Loops
- TypeScript - For Loop
- TypeScript - While Loop
- TypeScript - Do While Loop
- TypeScript Functions
- TypeScript - Functions
- TypeScript - Function Types
- TypeScript - Optional Parameters
- TypeScript - Default Parameters
- TypeScript - Anonymous Functions
- TypeScript - Function Constructor
- TypeScript - Rest Parameter
- TypeScript - Parameter Destructuring
- TypeScript - Arrow Functions
- TypeScript Interfaces
- TypeScript - Interfaces
- TypeScript - Extending Interfaces
- TypeScript Classes and Objects
- TypeScript - Classes
- TypeScript - Objects
- TypeScript - Access Modifiers
- TypeScript - Readonly Properties
- TypeScript - Inheritance
- TypeScript - Static Methods and Properties
- TypeScript - Abstract Classes
- TypeScript - Accessors
- TypeScript - Duck-Typing
- TypeScript Advanced Types
- TypeScript - Intersection Types
- TypeScript - Type Guards
- TypeScript - Type Assertions
- TypeScript Type Manipulation
- TypeScript - Creating Types from Types
- TypeScript - Keyof Type Operator
- TypeScript - Typeof Type Operator
- TypeScript - Indexed Access Types
- TypeScript - Conditional Types
- TypeScript - Mapped Types
- TypeScript - Template Literal Types
- TypeScript Generics
- TypeScript - Generics
- TypeScript - Generic Constraints
- TypeScript - Generic Interfaces
- TypeScript - Generic Classes
- TypeScript Miscellaneous
- TypeScript - Triple-Slash Directives
- TypeScript - Namespaces
- TypeScript - Modules
- TypeScript - Ambients
- TypeScript - Decorators
- TypeScript - Type Compatibility
- TypeScript - Date Object
- TypeScript - Iterators and Generators
- TypeScript - Mixins
- TypeScript - Utility Types
- TypeScript - Boxing and Unboxing
- TypeScript - tsconfig.json
- From JavaScript To TypeScript
- TypeScript Useful Resources
- TypeScript - Quick Guide
- TypeScript - Cheatsheet
- TypeScript - Useful Resources
- TypeScript - Discussion
TypeScript - Never
The never type
The never type in TypeScript represents the values that can never occur. For example, the return type of a function that throws an error is never.
function funcName(): never{ // it throws an exception or never returns }
You cannot assign a value to a variable of never type
let x: never; x = 10; // throw error
The above TypeScript code snippet will throw the following errors
Type 'number' is not assignable to type 'never'.
You can use the never type as the return type of a function that never returns or always throws an exception.
A function that never stops executing
function infiniteLoop(): never{ for(;;){} }
Another example of function that never stops executing
function infiniteLoop(): never{ while (true) { } }
Variables also acquire the type never when narrowed by any type guards that can never be true.
The never type is used when we have exhausted all possible value and we don't have anything to assign.
function check(value: string | number) { if (typeof value === 'string') { return "value is string"; } else { return "value is number"; } // here, not a string or number // "value" can't occur here, so it's type "never" }
The never type is a subtype of, and assignable to, every type; however, no type is a subtype of, or assignable to, never (except never itself). Even any isnt assignable to never.
You can annotate a variable with never type, but it is very uncommon
function showError(): never{ throw new Error("Error message from function with never as return type."); } let x: never = showError();
In the above Typescript code snippet, variable x is annotated with the never type. The variable x is assigned showError() function that itself has the never as return type.
void vs. never
In TypeScript, a variable of void type can store undefined as a value. On the other hand, never can't store any value.
let val1: void = undefined; let val2: never = undefined; // error: Type 'undefined' is not assignable to type 'never'.
We know that if a function in TypeScript does not return any value, it returns undefined. We generally annotate the type of return type of such function with void. Look at the below example,
function greet(): void{ console.log("Welcome to tutorials Point"); } let msg: void = greet(); console.log(msg);
In the above example, we defined a function greet() that doesn't return any value. When we call the function, the return value is undefined.
On compiling, it will generate the following JavaScript code.
function greet() { console.log("Welcome to tutorials Point"); } let msg = greet(); console.log(msg);
The output of the above code is as follows
Undefined