TypeScript Standard Library: Complete Overview
TypeScript is not just a superset of JavaScript that adds static typing. The language comes with a powerful standard library that expands developer capabilities. In this article, we will thoroughly examine the key components of the TypeScript standard library, show usage examples, and explain how they simplify development.
1. Built-in Data Types
The TypeScript standard library includes all basic JavaScript types but adds strict typing and new abstractions:
number,string,boolean— primitive types;nullandundefined— special values;any— disables type checking;unknown— a safe alternative toany;never— type for functions that never return;void— absence of a return value.
// Example of using built-in typeslet age: number = 25;let name: string = "Anna";let isActive: boolean = true;let data: unknown = { id: 1 };let error: never = (() => { throw new Error("Error"); })();2. Utility Types
TypeScript provides a set of built-in utility types that allow transforming existing types without writing complex constructs:
Partial<T>— makes all fields of typeToptional;Required<T>— makes all fields required;Readonly<T>— prevents modification of fields;Pick<T, K>— selects specified keys from a type;Omit<T, K>— excludes specified keys;Record<K, T>— creates a type with keysKand valuesT.
interface User { id: number; name: string; email: string;}
// Make all fields optionaltype PartialUser = Partial<User>;
// Select only id and nametype UserPreview = Pick<User, "id" | "name">;
// Create a dictionarytype UserMap = Record<string, User>;3. Working with Collections: Map, Set, WeakMap, WeakSet
The standard library includes typed versions of ES6+ collections. TypeScript automatically infers types when using these structures:
Map<K, V>— key-value collection;Set<T>— collection of unique values;WeakMap<K extends object, V>— Map analog with "weak" references;WeakSet<T extends object>— Set analog for objects.
// Typed Mapconst userRoles = new Map<number, string>();userRoles.set(1, "admin");userRoles.set(2, "user");
// Typed Setconst uniqueIds = new Set<number>([1, 2, 3, 3]); // {1, 2, 3}4. Promises and Asynchrony
TypeScript fully supports asynchronous programming through Promise<T> and the async/await keywords. Typing promises allows you to specify exactly what type of data will be returned:
async function fetchUser(id: number): Promise<User> { const response = await fetch(`/api/users/${id}`); return response.json(); // Return type — User}
// UsagefetchUser(1).then(user => { console.log(user.name); // name — string});5. Working with DOM and Browser APIs
The TypeScript standard library includes full typing for the DOM API. This allows safe work with elements, events, and other browser objects:
Also in library
Working with S3 and Cloud Storage in Kotlin: AWS SDK and Ktor S3 Client
ORM for PHP: Comparison of Doctrine, Eloquent, Propel, and RedBeanPHP
HTTP Client for Go (Golang): Top Libraries for Network Operations
Machine Learning in C++: Overview of Libraries for Development
Carbon for PHP: A Complete Guide with Examples
Recommendations