TypeScript Standard Library: Complete Overview

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

TypeScript Standard Library: Complete Overview



TypeScript is a strictly typed superset of JavaScript that compiles to plain JS. However, its power lies not only in its type system. The built-in TypeScript standard library provides powerful tools for working with types, utilities, and metaprogramming. In this article, we will break down the key components of the TypeScript standard library, show examples of their use, and give practical advice.



What is the TypeScript Standard Library?

The TypeScript standard library is a set of built-in types, interfaces, and utilities that come bundled with the compiler. It includes:

  • Basic types (string, number, boolean, any, unknown, never, void).
  • Utility types (Partial, Required, Pick, Omit, Record, and others).
  • Decorators (an experimental feature for metaprogramming).
  • Namespaces (namespace) and modules.
  • Advanced types (Promise, Map, Set, WeakMap, WeakSet).
  • Types for working with the DOM (included via lib: ["dom"]).


Utility Types: The Heart of the Library

Utility types allow you to transform existing types without duplicating code. Let's look at the most popular ones:



Partial and Required

interface User {  id: number;  name: string;  email: string;}

// All fields become optionaltype PartialUser = Partial<User>;

// All fields become required (if they were optional)type RequiredUser = Required<PartialUser>;


Pick and Omit

interface Product {  id: number;  title: string;  price: number;  description: string;}

// Select only the needed fieldstype ProductPreview = Pick<Product, 'id' | 'title'>;

// Exclude unnecessary fieldstype ProductWithoutDesc = Omit<Product, 'description'>;


Record and Readonly

// Creates an object with specific keys and valuestype UserRoles = Record<'admin' | 'user' | 'guest', string>;

// Makes all fields read-onlytype ReadonlyUser = Readonly<User>;


Working with Promise and Asynchrony

TypeScript provides built-in types for working with asynchronous code:

async function fetchData(): Promise<string> {  return "data loaded";}

// Type for the Promise resulttype DataType = Awaited<ReturnType<typeof fetchData>>; // string


Decorators: Metaprogramming in TypeScript

Decorators are special declarations that allow you to annotate and modify classes, methods, properties, and parameters. They are actively used in frameworks (Angular, NestJS).



Class Decorator Example

function sealed(constructor: Function) {  Object.seal(constructor);  Object.seal(constructor.prototype);}

@sealedclass Calculator { add(a: number, b: number): number { return a + b; }}


Method Decorator Example

function log(target: any, key: string, descriptor: PropertyDescriptor) {  const original = descriptor.value;  descriptor.value = function (...args: any[]) {    console.log(`Method ${key} called with arguments: ${args}`);    return original.apply(this, args);  };  return descriptor;}

class UserService { @log getUser(id: number): string { return `User ${id}`; }}


Namespaces

Namespaces help organize code and avoid

Recommendations