Skip to main content

TypeScript visual mental model

TypeScript improves React code by making contracts explicit before runtime. React still owns rendering and runtime behavior; TypeScript helps you catch invalid shapes, impossible variants, and incorrect API usage earlier.

TypeScript's role in ReactStatic contracts guide development; runtime behavior still belongs to React and the browser.
Component contract
props ยท events ยท children ยท refs ยท generic constraints
TypeScript verifies usage
Call sites and implementations must agree on the contract.
React executes runtime behavior
Rendering, state, Effects, Suspense, DOM behavior, and network results still happen at runtime.

Where types add the most valueโ€‹

React type-safety map
Props
Public component inputs and variant constraints.
State
Valid state shapes and updater contracts.
Events
Correct DOM event types and handler signatures.
Context
Shared value contracts and nullability boundaries.
Reducers
Discriminated actions and exhaustive transitions.
Refs
Element/handle types and ownership of imperative APIs.

Inference first, annotation where it communicates designโ€‹

Should you write an explicit type here?
Local value is obvious from initializationPrefer inference
Component exposes a public prop APIDefine an explicit contract
State has multiple meaningful variantsModel the variants explicitly
Reducer actions represent domain eventsUse a discriminated union
Boundary receives unknown external dataValidate at runtime, then narrow the type

Types do not validate runtime inputโ€‹

Static type vs runtime trust boundary
Compile timeTypeScript
Checks code you control against declared types.
RuntimeExternal input
API payloads, form data, storage, URL params, and user input can still be invalid.
Runtime validation + typed domain value
Validate first; then let TypeScript help inside the trusted application boundary.

Model impossible UI states awayโ€‹

Prefer one valid state model over many unrelated booleans
Weak modelIndependent flags
isLoading, hasError, hasData can accidentally describe contradictory states.
Stronger modelDiscriminated variants
idle | loading | success | error makes allowed states explicit.

Reusable component APIsโ€‹

1Define domain variants
2Encode prop relationships
3Infer local details
4Expose narrow public types
5Validate runtime boundaries

Keep this mental modelโ€‹

TypeScript in React in one picture
Contracts
Describe what callers may provide.
Inference
Reduce annotation noise.
Narrowing
Make variants explicit.
Validation
Protect runtime boundaries.

Continue with React with TypeScript โ€” Components, Props, Children, and Events for the detailed APIs and production patterns.