npx tsc --initGenerates a starter `tsconfig.json` file with common compiler options.
Core TypeScript syntax, typing patterns, and compiler commands.
Project setup, compiler usage, and quick inspection.
npx tsc --initGenerates a starter `tsconfig.json` file with common compiler options.
npx tsc --versionUseful when debugging behavior differences across TypeScript releases.
npx tsc --noEmitCommon CI command for catching errors without writing build output.
npx tsc --watchKeeps the compiler running and recompiles on file changes.
npx tsc -p tsconfig.jsonPass `-p` or `--project` to point TypeScript at a specific config file or directory.
npx tsc --buildBuild mode is intended for composite projects and project references.
npx tsc --explainFilesUseful for debugging include/exclude and file graph issues.
npx tsc --traceResolutionUseful when path aliases or package resolution behave unexpectedly.
Basic types, annotations, inference, and literal behavior.
const appName: string = "CheatSheet";
const port: number = 3000;
const isProd: boolean = false;Type annotations are optional in many cases because TypeScript can infer them.
const ids: number[] = [1, 2, 3];
const names: Array<string> = ["Ada", "Linus"];Both array syntaxes are common; choose one style and stay consistent.
const user: { id: number; name: string; admin?: boolean } = {
id: 1,
name: "Ada"
};Optional properties use `?` in object type annotations.
let value: string | number;
value = "ok";
value = 42;Union types are one of the most common TypeScript patterns.
type Theme = "light" | "dark" | "system";
const theme: Theme = "dark";Literal unions are ideal for config values, modes, and command flags.
const count = 10; // number
const title = "Hello"; // stringInference usually produces cleaner code than over-annotating every value.
const statusMap = {
ok: 200,
notFound: 404,
} as const;`as const` narrows values to their literal types and makes properties readonly.
Reusable object shapes and composition patterns.
interface User {
id: number;
name: string;
email?: string;
}Interfaces are commonly used for object shapes, especially public APIs.
type UserId = string | number;
type Point = { x: number; y: number };Type aliases are more flexible than interfaces for unions and mapped types.
interface Person {
name: string;
}
interface Employee extends Person {
team: string;
}`extends` helps model shared fields without duplication.
type Audit = { createdAt: Date };
type Customer = { id: string; name: string };
type AuditedCustomer = Customer & Audit;Intersection types are useful when enriching objects with shared metadata.
type Config = {
readonly apiBaseUrl: string;
readonly retries: number;
};Readonly protects values from reassignment after initialization.
type StringMap = {
[key: string]: string;
};Index signatures are useful for config maps and lookup tables.
Position-based arrays and enum patterns.
const point: [number, number] = [10, 20];Tuples are useful when position matters and object keys would be excessive.
type HttpResponse = [status: number, body: string];Tuple labels improve editor help without changing runtime behavior.
enum Direction {
Up,
Down,
Left,
Right,
}Enums exist in TypeScript, though many teams prefer literal unions for simpler output.
enum Status {
Draft = "draft",
Published = "published",
}String enums are easier to debug than numeric enums in serialized output.