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.
Basic Types 8
let name: string = "John"let age: number = 30let isActive: boolean = truelet items: string[] = ["a", "b"]let tuple: [string, number] = ["a", 1]let value: any = "anything"let value: unknown = "safe"let value: neverInterfaces & Types 5
interface User { name: string; age: number }type User = { name: string; age: number }interface Admin extends User { role: string }type Status = "active" | "inactive"type Result = string | null | undefinedGenerics 4
function identity<T>(arg: T): T { return arg }interface Container<T> { value: T }type Pair<T, U> = [T, U]function getFirst<T>(arr: T[]): TUtility Types 6
Partial<User>Required<User>Readonly<User>Pick<User, "name" | "age">Omit<User, "age">Record<string, number>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