TypeScript Cheatsheet - Type Types & Generics Reference

This reference is for JS and frontend developers who want to write clear types in their day-to-day code. Instead of dumping definitions, entries progress from variables to interfaces, generics, and utility types, with each explaining the scenario it solves — such as when to prefer interface over type, how Partial makes form fields optional, and how Pick/Omit carve a subset out of a larger type. After reading you should be able to annotate functions and objects precisely, constrain values with literal unions, and use utility types to tighten your options.

Languages·23 commands·Last updated 2026-07-21
typescripttsTypesFrontend

Basic Types 8

let name: string = "John"
String type
let age: number = 30
Number type
let isActive: boolean = true
Boolean type
let items: string[] = ["a", "b"]
Array type
let tuple: [string, number] = ["a", 1]
Tuple type
let value: any = "anything"
Any type (use with caution)
let value: unknown = "safe"
Unknown type (safe)
let value: never
Never type

Interfaces & Types 5

interface User { name: string; age: number }
Define an interface
type User = { name: string; age: number }
Type alias
interface Admin extends User { role: string }
Interface inheritance
type Status = "active" | "inactive"
Union type
type Result = string | null | undefined
Nullable type

Generics 4

function identity<T>(arg: T): T { return arg }
Generic function
interface Container<T> { value: T }
Generic interface
type Pair<T, U> = [T, U]
Multi-parameter generics
function getFirst<T>(arr: T[]): T
Generic constraint

Utility Types 6

Partial<User>
All properties optional
Required<User>
All properties required
Readonly<User>
All properties readonly
Pick<User, "name" | "age">
Pick a subset of properties
Omit<User, "age">
Omit specific properties
Record<string, number>
Key-value pair type

Tips

  • unknown is safer than any — it requires type checking or type assertion before use.
  • interface supports declaration merging, type does not; type supports unions and mapped types.
  • as const makes object properties literal types, e.g., { role: "admin" as const }.
  • The satisfies operator (TS 4.9+) validates types while preserving the inferred type.

FAQ

When should I use TypeScript interface over type?

They are mostly interchangeable; type can also express unions, tuples and mapped types that interface cannot. Prefer interface for objects meant to be extended, implemented or publicly exported (it supports declaration merging); use type for unions, intersections, and conditional or mapped type gymnastics. Stay consistent within a codebase.

What is the difference between any and unknown in TypeScript?

any disables type checking — assignable to anything and freely accessible, bypassing the type system; unknown means not yet known, only receiving assignments and requiring narrowing (typeof, instanceof or a cast) before use. Prefer unknown over any for safer code.

What does a generic constraint (extends) do?

A constraint such as <T extends {length: number}> limits T to types satisfying that structure, so the function can safely access t.length without a type error. Constraints can combine, and the resulting return type keeps the concrete inferred type.

How do Partial, Pick and Omit differ?

Partial<T> makes every property of T optional, handy when some form fields are still empty; Pick<T, K> extracts only keys K into a new type; Omit<T, K> drops keys K and keeps the rest. All three are compile-time only and leave runtime objects unchanged.

When should I use a type assertion (value as T) in TypeScript?

An as assertion tells the compiler to trust your knowledge of the type; use it when you genuinely know more, such as narrowing data parsed from any or JSON. Overusing it hides real bugs — prefer inferable types or runtime checks (like hasOwnProperty) before asserting. A non-null assertion expr! only strips null/undefined and throws at runtime if the value actually is empty.

Official References

Each command links to its official documentation below, so you can verify the latest usage and read deeper.

Maintained by LaoHand

Publicly updated on Jul 21, 2026, continuously proofread against official docs.

Contact Us

Wrong command or description? Send us corrections, business inquiries or product feedback by email.

Contact Us