Skip to content

TypeScript

Status: 🌱

Motivation

Create safer application layers with strong typing across frontend and backend boundaries.

Starter Points

  • Enforce strict compiler settings and avoid implicit any.
  • Define shared contract types for API requests/responses.
  • Improve runtime validation at external boundaries.

Type System Model

  • TypeScript uses structural typing: compatibility is based on shape, not on explicit nominal declarations (contrast: Rust is nominal, based on declared type identity).
  • Type information is mostly erased after transpilation, so runtime checks must use JavaScript values (properties, prototypes, tags).
  • Libraries such as Axios expose guards like axios.isAxiosError to narrow unknown values in a structurally safe way.
  • Useful Go parallel: interfaces are also structural ("implements by method set"), but this is a compile-time/runtime model tied to Go's own type system, not erased transpiled metadata.

Exception Handling

  • In catch, prefer unknown over any so type checks are required before property access.
  • Use type guards from libraries (or custom guards) before reading error fields.
  • Rethrow errors you are not explicitly handling; only swallow known, intentional cases.
try {
  // ...
} catch (err: unknown) {
  if (axios.isAxiosError(err)) {
    console.log(err.response?.status);
    return;
  }
  throw err;
}