TypeScript Standard Library: Complete Overview
TypeScript is a strictly typed superset of JavaScript that adds static typing and powerful tools for developing large applications. Unlike many languages, TypeScript does not have an extensive built-in standard library in the classical sense (like Java or Python). Instead, it provides a set of built-in types, type utilities, decorators, and compiler tools. In this article, we will thoroughly examine all key components of the TypeScript standard library.
1. Built-in Data Types
TypeScript includes all primitive JavaScript types and adds its own:
boolean— logical typenumber— numbers (integers and floating-point)string— stringsnullandundefined— special valuessymbol— unique identifiersbigint— large integersany— disables type checkingunknown— safe alternative to anynever— type for functions that never completevoid— absence of return value
Example of using built-in types:
let isDone: boolean = false;let decimal: number = 42;let color: string = "blue";let notSure: unknown = 4;notSure = "now a string"; // allowed
function error(message: string): never { throw new Error(message);}2. Utility Types
TypeScript provides powerful built-in utilities for type transformation. Here are the most useful ones:
- Partial<T> — makes all fields of type T optional
- Required<T> — makes all fields required
- Readonly<T> — makes all fields read-only
- Pick<T, K> — selects specified keys from a type
- Omit<T, K> — excludes specified keys
- Record<K, T> — creates an object type with keys K and values T
- Exclude<T, U> — excludes types present in U
- ReturnType<T> — extracts the return type of a function
Example of using utility types:
interface User { id: number; name: string; email: string;}
// Partial user updatefunction updateUser(id: number, changes: Partial<User>): void { // Update logic}
// Read-onlyconst readonlyUser: Readonly<User> = { id: 1, name: "Ivan", email: "ivan@example.com"};
// Selecting fieldstype UserName = Pick<User, "name" | "email">;3. Decorators
Decorators are special declarations that allow modifying classes, methods, properties, and parameters. TypeScript supports several types of decorators:
- Class decorators — applied to the class constructor
- Method decorators — applied to class methods
- Property decorators — applied to properties
- Parameter decorators — applied to method parameters
Example of a method decorator for logging:
function log(target: any, propertyKey: string, descriptor: PropertyDescriptor) { const originalMethod = descriptor.value; descriptor.value = function (...args: any[]) { console.log(`Calling ${propertyKey} with arguments: ${JSON.stringify(args)}`); return originalMethod.apply(th