Annotate a Variable Type
let name: string = "Ada";Declares the type of a variable.
TypeScript syntax quick reference for types, interfaces and generics, from the official TypeScript Handbook. Every item below is clearly marked as a shortcut, command, or syntax reference.
let name: string = "Ada";Declares the type of a variable.
function add(a: number, b: number): number {
return a + b;
}Types a function's parameters and return value.
interface User {
id: number;
name: string;
}Describes the shape of an object.
const nums: number[] = [1, 2, 3];Types an array of values.
type ID = string | number;Names a type so it can be reused.
let id: string | number;Allows a value to be one of several types.
string, number, boolean, bigint, symbol, null, undefinedThe built-in primitive types.
const add = (a: number, b: number): number => a + b;Types an arrow function.
Declares the type of a variable.
let name: string = "Ada";Types a function's parameters and return value.
function add(a: number, b: number): number {
return a + b;
}Describes the shape of an object.
interface User {
id: number;
name: string;
}Types an array of values.
const nums: number[] = [1, 2, 3];Names a type so it can be reused.
type ID = string | number;Allows a value to be one of several types.
let id: string | number;The built-in primitive types.
string, number, boolean, bigint, symbol, null, undefinedTypes an arrow function.
const add = (a: number, b: number): number => a + b;Makes a function parameter optional.
function greet(name?: string) { ... }Marks an object property as optional.
interface User {
email?: string;
}Writes a function that works with any type.
function first<T>(arr: T[]): T {
return arr[0];
}Tells the compiler to treat a value as a type.
const el = document.getElementById("x") as HTMLInputElement;Restricts a value to specific literals.
type Dir = "left" | "right";Builds an interface from another.
interface Admin extends User {
role: string;
}Defines a set of named constants.
enum Color {
Red,
Green,
}Narrows a union with a typeof check.
if (typeof id === "string") { ... }Makes all properties optional.
Partial<User>Types an object with set keys and values.
Record<string, number>Defines a class with typed fields.
class Dog {
constructor(public name: string) {}
}Types the result of an async function.
async function load(): Promise<User[]> { ... }Type-checks and compiles TypeScript files.
tscGives a parameter a default value.
function greet(name = "friend") { ... }Types a fixed-length array.
const point: [number, number] = [1, 2];Prevents a property from being reassigned.
interface Point {
readonly x: number;
}Combines several types into one.
type Admin = User & { role: string };Writes a reusable typed container.
interface Box<T> {
value: T;
}Tells the compiler a value isn't null or undefined.
el!.focus();A safe alternative to any that must be narrowed.
let value: unknown;Creates a type from a value.
type Config = typeof config;Creates a union of an object type's keys.
type Keys = keyof User;Keeps only chosen properties.
Pick<User, "id" | "name">Removes chosen properties.
Omit<User, "password">Imports a type without importing runtime code.
import type { User } from "./types";Creates a tsconfig.json with default settings.
tsc --initNarrows by checking for a property.
if ("swim" in animal) { ... }Writes a function that narrows a type.
function isFish(p: Fish | Bird): p is Fish {
return "swim" in p;
}It covers 36 common TypeScript patterns and examples, including Annotate a Variable Type, Typed Function, Define an Interface, Array Type, Define a Type Alias.
Yes. Each reference page includes a copyable code example.
Check the linked documentation for language or runtime version requirements. Libraries, file paths, terminal quoting and environment settings can differ across systems; no blanket platform compatibility is implied.