---
title: "TypeScript 6 for Angular applications"
chapter: "01"
---

# TypeScript 6 for Angular applications

TypeScript is Angular's compile-time language layer. It describes shapes and
catches mismatches, then erases types to JavaScript for the browser.

## Strict foundations

Enable strict TypeScript and Angular template checking. Avoid using `any` as a
silence button. At untrusted boundaries, accept `unknown`, validate, and narrow.

```ts
type LoadState<T> =
  | { kind: 'idle' }
  | { kind: 'loading' }
  | { kind: 'ready'; value: T }
  | { kind: 'error'; message: string };
```

Discriminated unions make illegal UI states harder to represent and allow
exhaustive switches.

## Interfaces and types

Use interfaces for open object contracts and types for unions, mappings, and
composition. Prefer domain names over transport names. An API response type is
not automatically a valid domain model.

## Generics

Generics relate input and output types. Keep constraints meaningful. A generic
`ApiResponse<T>` may help at the transport boundary; a generic service with six
type parameters may hide a poor abstraction.

## Runtime validation

Types do not validate JSON. Use deliberate parsing, schema validation, or
mapping before treating remote data as trusted. Dates arrive as strings unless
you convert them. Numeric IDs can lose precision.

## Immutability

Readonly types prevent accidental assignment at compile time; they do not deep-
freeze runtime objects. Use immutable update patterns with signals and state so
change is explicit.

## Async types

Distinguish `Promise<T>`, `Observable<T>`, `Signal<T>`, and `Resource<T>`.
They model different time and ownership semantics. Convert at clear boundaries,
not repeatedly throughout templates.

## Feynman check

TypeScript is a careful proofreader that leaves before the browser runs the
story. Explain what security and validation work must remain at runtime.
