Loading lesson path
Concept visual
Start at both ends
TypeScript includes several special types that have specific behaviors in the type system. These types are used in various scenarios to handle cases where the type might not be known in advance or when you need to work with JavaScript primitives in a type-safe way.
Formula
These special types are part of TypeScript's type system and help make your code more type - safe and self - documenting.The any type is the most flexible type in TypeScript. It essentially tells the compiler to skip type checking for a particular variable. While this can be useful in certain situations, it should be used sparingly as it bypasses TypeScript's type safety features.
When working with dynamic content where the type is unknown When you need to opt out of type checking for a specific case The example below does not use any and will throw an error:
Example without any let u = true;
u = "string"; // Error: Type 'string' is not assignable to type 'boolean'.
Math.round(u); // Error: Argument of type 'boolean' is not assignable to parameter of type 'number'.Setting a variable to the special type any disables type checking:
Example with any let v: any = true;
v = "string"; // no error as it can be "any" type
Math.round(v); // no error as it can be "any" type any can be a useful way to get past errors since it disables type checking, butTypeScript will not be able to provide type safety, and tools which rely on type data, such as auto completion, will not work. Remember, it should be avoided at "any" cost...
Formula
The unknown type is a type - safe counterpart of any.
It's the type - safe way to say "this could be anything, so you must perform some type of checking before you use it".Formula
unknown must be type - checked before useYou can't access properties on an unknown type without type assertion You can't call or construct values of type unknown TypeScript will prevent unknown types from being used without proper type checking, as shown in the example below:
let w: unknown = 1;
w = "string"; // no error w = {
runANonExistentMethod: () => {
console.log("I think therefore I am");
}
} as { runANonExistentMethod: () => void}
// How can we avoid the error for the code commented out below when we don't know the type?
// w.runANonExistentMethod(); // Error: Object is of type 'unknown'.
if(typeof w === 'object' && w !== null) {
(w as { runANonExistentMethod: Function }).runANonExistentMethod();
}// Although we have to cast multiple times we can do a check in the if to secure our type and have a safer casting
When working with data from external sources (APIs, user input, etc.) When you want to ensure type safety while still allowing flexibility
Formula
When migrating from JavaScript to TypeScript in a type - safe wayYou can narrow down the type of an unknown value using type guards:
function processValue(value: unknown) {
if (typeof value === 'string') {
// value is now treated as string console.log(value.toUpperCase());
} else if (Array.isArray(value)) {
// value is now treated as any[]
console.log(value.length);
}
}The never type represents the type of values that never occur. It's used to indicate that something never happens or should never happen.
Functions that never return (always throw an error or enter an infinite loop)
1. Function that never returns function throwError(message: string): never {
throw new Error(message);
}2. Exhaustiveness checking with discriminated unions type Shape = Circle | Square | Triangle;
function getArea(shape: Shape): number {
switch (shape.kind) {
case 'circle':
return Math.PI * shape.radius ** 2;
case 'square':
return shape.sideLength ** 2;default:
Formula
// TypeScript knows this should never happen const _exhaustiveCheck: never = shape;return _exhaustiveCheck;
}
}let x: never = true; // Error: Type 'boolean' is not assignable to type 'never'.For functions that will never return a value
In generic types to indicate certain cases are impossible Type: undefined & null In TypeScript, both undefined and null have their own types, just like string or number. By default, these types can be assigned to any other type, but this can be changed with TypeScript's strict null checks.
undefined means a variable has been declared but not assigned a value null is an explicit assignment that represents no value or no object In TypeScript, both have their own types: undefined and null respectively With strictNullChecks enabled, you must explicitly handle these types
Formula
Basic Usage let y: undefined = undefined;let z: null = null;// Optional parameter (implicitly string | undefined) function greet(name?: string) {
return `Hello, ${name || 'stranger'}`;
}
// Optional property in an interface interface User {
name: string;
age?: number; // Same as `number | undefined`
}// Nullish coalescing (??) - only uses default if value is null or undefined const value = input ?? 'default';
// Optional chaining (?.) - safely access nested properties const street = user?.address?.street;These types are most useful when strictNullChecks is enabled in your tsconfig.json file. This ensures that null and undefined are only assignable to themselves and any. To enable strict null checks, add this to your tsconfig.json
{
"compilerOptions": {
"strictNullChecks": true
}
}