skip to content
Johannes Binder

TypeScript Tips for Everyday Use

/ 1 min read

Table of Contents

TypeScript has become the default for many JavaScript projects. Here are some tips I use daily.

Utility Types

TypeScript ships with powerful utility types that save you from writing boilerplate:

// Instead of manually making fields optional
type PartialUser = Partial<User>;
// Pick only what you need
type UserPreview = Pick<User, "name" | "avatar">;
// Exclude fields
type PublicUser = Omit<User, "password" | "email">;

Const Assertions

Use as const to narrow types to their literal values:

const config = {
theme: "dark",
lang: "en",
} as const;

Discriminated Unions

Model your state explicitly instead of relying on optional fields:

type Result<T> =
| { status: "success"; data: T }
| { status: "error"; message: string };

This makes impossible states unrepresentable.