← All guides

Programmer’s Reference

TypeScript

Learn JavaScript control flow with types, interfaces, and compile-time checks.

TypeScript 5+; satisfies requires 4.9+. Type annotations are erased during compilation and do not validate external data at runtime. Code examples are independent .ts files, checked with strict mode and an ES2020 target.

20 entries · Essentials first · Reviewed October 5, 2026

Glossary: commands, keywords & terms

Select a term for its definition, syntax, example, and common mistakes.

Getting started

tsc / --noEmit / --strict

Use an already installed project-local compiler to check types without generating JavaScript.

Terminal
./node_modules/.bin/tsc --version
./node_modules/.bin/tsc --strict --noEmit --target ES2020 example.ts

Practical usePractice in a standalone example file before using this pattern in your project.

Common mistakesInstall TypeScript as a development dependency first. Passing files directly does not read tsconfig.json; use tsc --project . for project settings.

Source: www.typescriptlang.org documentation

tsconfig.json / strict / noEmit

Configure project-wide type checking. noEmit produces no compiled files.

JSON configuration
{
  "compilerOptions": {
    "target": "ES2020",
    "strict": true,
    "noEmit": true
  },
  "include": ["src/**/*.ts"]
}

Practical usePractice in a standalone example file before using this pattern in your project.

Common mistakesRun the project compiler with --project .; emitting JavaScript needs a separate build configuration or tool.

Source: www.typescriptlang.org documentation

Keywords and types

const / let / type annotations

Declare variables and optionally specify their types.

TypeScript code
const displayName: string = 'Demo';
let count: number = 0;
const ready: boolean = true;
count += 1;
console.log(displayName, count, ready);

Practical usePractice in a standalone example file before using this pattern in your project.

Common mistakesUse lowercase string, number, and boolean. const prevents reassignment, not mutation inside objects.

Source: www.typescriptlang.org documentation

if / else / typeof

Branch on a condition; typeof checks can narrow a union type.

TypeScript code
function label(value: string | number): string {
  if (typeof value === 'number') {
    return value.toFixed(0);
  } else {
    return value.toUpperCase();
  }
}
console.log(label(3));

Practical usePractice in a standalone example file before using this pattern in your project.

Common mistakesA type annotation is not a runtime check. typeof null is "object".

Source: www.typescriptlang.org documentation

|| / && / !

Use short-circuit boolean logic inherited from JavaScript.

TypeScript code
const nickname: string = '';
const label = nickname || 'Guest';
const ready: boolean = true;
if (ready && !nickname) { console.log(label); }

Practical usePractice in a standalone example file before using this pattern in your project.

Common mistakes|| returns an operand, not necessarily a boolean. Zero and empty strings are falsey.

Source: www.typescriptlang.org documentation

?? / ?. / null / undefined

Use optional chaining for a missing object and nullish coalescing for a fallback.

TypeScript code
function display(user: { name?: string } | undefined): string {
  return user?.name ?? 'Guest';
}
console.log(display(undefined));

Practical usePractice in a standalone example file before using this pattern in your project.

Common mistakes?? preserves zero and empty strings. Optional chaining does not make every subsequent property access safe.

Source: www.typescriptlang.org documentation

for / for...of / while

Repeat work with JavaScript loop syntax and typed values.

TypeScript code
const names: string[] = ['Demo', 'Example'];
for (const name of names) { console.log(name); }
for (let i = 0; i < names.length; i++) { console.log(i); }
let remaining = 2;
while (remaining > 0) { remaining--; }

Practical usePractice in a standalone example file before using this pattern in your project.

Common mistakesfor...in visits property keys, while for...of visits iterable values. Avoid infinite loops.

Source: www.typescriptlang.org documentation

break / continue

Exit the enclosing loop or skip its current iteration.

TypeScript code
for (let n = 0; n < 5; n++) {
  if (n === 1) { continue; }
  if (n === 3) { break; }
  console.log(n);
}

Practical usePractice in a standalone example file before using this pattern in your project.

Common mistakesbreak does not return from a function; continue requires a loop.

Source: www.typescriptlang.org documentation

function / return / void

Describe function inputs and output; void means no useful returned value.

TypeScript code
function doubleValue(n: number): number { return n * 2; }
function report(message: string): void { console.log(message); }
report(String(doubleValue(3)));

Practical usePractice in a standalone example file before using this pattern in your project.

Common mistakesA return annotation is checked at compile time; it does not convert values.

Source: www.typescriptlang.org documentation

type / union / literal types

Give a type a reusable name and restrict values to alternatives.

TypeScript code
type State = 'ready' | 'waiting';
type Identifier = string | number;
const state: State = 'ready';
const id: Identifier = 7;
console.log(state, id);

Practical usePractice in a standalone example file before using this pattern in your project.

Common mistakesA type alias does not create a runtime value. Narrow unions before using type-specific methods.

Source: www.typescriptlang.org documentation

interface / optional properties / readonly

Describe an object shape, including optional and read-only properties.

TypeScript code
interface Item {
  readonly id: number;
  name: string;
  note?: string;
}
const item: Item = { id: 1, name: 'Demo' };
console.log(item.note ?? 'No note');

Practical usePractice in a standalone example file before using this pattern in your project.

Common mistakesreadonly is a type-checking restriction, not a runtime freeze. Optional properties may be undefined.

Source: www.typescriptlang.org documentation

Array<T> / T[] / tuples

Type a variable-length array or a fixed-position tuple.

TypeScript code
const names: Array<string> = ['Demo'];
const counts: number[] = [1, 2];
const pair: [string, number] = ['Demo', 2];
console.log(names, counts, pair);

Practical usePractice in a standalone example file before using this pattern in your project.

Common mistakes[number] is a one-element tuple, not a general array. Runtime arrays can still contain invalid external data.

Source: www.typescriptlang.org documentation

unknown / any

unknown requires narrowing before use; any disables most type checking.

TypeScript code
function describe(value: unknown): string {
  return typeof value === 'string' ? value.toUpperCase() : 'Not text';
}
console.log(describe('Demo'));

Practical usePractice in a standalone example file before using this pattern in your project.

Common mistakesPrefer unknown for untrusted values. Neither unknown nor any validates a JSON schema.

Source: www.typescriptlang.org documentation

as / type assertions

Tell the compiler how to treat a value without changing it.

TypeScript code
const value: unknown = 'Demo';
const text = value as string;
console.log(text.toUpperCase());

Practical usePractice in a standalone example file before using this pattern in your project.

Common mistakesAssertions do not validate or convert values; this succeeds only because the example value really is a string.

Source: www.typescriptlang.org documentation

<T> / generics

Preserve a relationship between input and output types.

TypeScript code
function first<T>(values: T[]): T | undefined {
  return values[0];
}
const result = first<string>(['Demo']);
console.log(result);

Practical usePractice in a standalone example file before using this pattern in your project.

Common mistakesAn array may be empty, so this return type includes undefined.

Source: www.typescriptlang.org documentation

keyof / indexed access types

Restrict keys to an object type and derive the corresponding value type.

TypeScript code
function get<T, K extends keyof T>(object: T, key: K): T[K] {
  return object[key];
}
console.log(get({ name: 'Demo', count: 3 }, 'name'));

Practical usePractice in a standalone example file before using this pattern in your project.

Common mistakeskeyof is a type-level operation; Object.keys returns runtime strings with different guarantees.

Source: www.typescriptlang.org documentation

satisfies / as const

Check compatibility while retaining a useful inferred type; as const preserves literal values.

TypeScript code
const settings = { mode: 'ready', retries: 3 } as const satisfies {
  mode: 'ready' | 'waiting';
  retries: number;
};
console.log(settings.mode);

Practical usePractice in a standalone example file before using this pattern in your project.

Common mistakessatisfies requires TypeScript 4.9+ and does not perform runtime validation.

Source: www.typescriptlang.org documentation

switch / case / never

Narrow a discriminated union and check that every variant is handled.

TypeScript code
type Result = { kind: 'ok'; value: number } | { kind: 'error'; message: string };
function describe(result: Result): string {
  switch (result.kind) {
    case 'ok': return String(result.value);
    case 'error': return result.message;
    default: { const unreachable: never = result; return unreachable; }
  }
}
console.log(describe({ kind: 'ok', value: 3 }));

Practical usePractice in a standalone example file before using this pattern in your project.

Common mistakesKeep a literal discriminator on each variant. Adding a variant requires updating exhaustive switches.

Source: www.typescriptlang.org documentation

async / await / Promise<T>

Express asynchronous results with a typed promise.

TypeScript code
async function getCount(): Promise<number> { return 3; }
async function main(): Promise<void> {
  const count = await getCount();
  console.log(count);
}
void main().catch(console.error);

Practical usePractice in a standalone example file before using this pattern in your project.

Common mistakesawait requires an async function or a compatible top-level module setting; handle promise rejection.

Source: www.typescriptlang.org documentation

goto (not supported)

TypeScript has no goto statement; use structured branches, loops, and functions.

TypeScript code
function describe(ready: boolean): string {
  if (ready) { return 'Ready'; }
  return 'Waiting';
}
console.log(describe(true));

Practical usePractice in a standalone example file before using this pattern in your project.

Common mistakesTypeScript uses JavaScript control-flow syntax; labels are not goto targets.

Source: www.typescriptlang.org documentation

Back to top ↑