TypeScript Types

Published Updated

Types are the part of TypeScript you actually write all day. This topic covers the type system from the first annotation to the point where you can model real application data: primitives and objects, named shapes, unions with narrowing, and the safe handling of data you have not verified yet.

The four guides below build on each other in order. If you have never written a TypeScript annotation, start at the top. If you already write annotations comfortably, jump straight to unions and narrowing, which is where the type system starts doing work JavaScript cannot.

TypeScript Types Topics

The Rule Everything Here Depends On

Every guide in this topic assumes one fact: TypeScript types exist only at compile time. The compiler reads your annotations, checks the code against them, and then removes them. The JavaScript that ships contains no trace of the type layer, which you can see by compiling a small file:

// input: user.ts
type User = { id: string; name: string };

const user: User = { id: "u1", name: "Ada" };
console.log(user.name);
// output: user.js, after running tsc
const user = { id: "u1", name: "Ada" };
console.log(user.name); // prints: Ada

The type declaration and the annotation vanished entirely. This erasure is why types cost nothing at runtime, and it is also why they cannot protect you from data that arrives at runtime. Both halves of that trade matter, and the guides return to them repeatedly.

There are two small exceptions worth knowing now so they never surprise you. enum and namespace declarations compile to real JavaScript objects, so they do survive compilation. Everything else in the type layer, including interfaces, aliases, unions, and generics, is erased.

Common Questions

Do TypeScript types exist when the code runs?

No, with narrow exceptions. Annotations, interfaces, aliases, and unions are erased during compilation, so the running JavaScript never sees them. Enums and namespaces are the exceptions because they compile to real JavaScript objects that do exist at runtime.

Do you have to annotate everything?

No. TypeScript infers most local types from the values you assign, and fighting that inference produces noisy code. Annotate the places that carry intent: function parameters, exported function returns, and shared data shapes. The first-types guide covers where the line sits.

Can a type check data from an API?

No. A type annotation is a compile-time claim, and API data arrives at runtime after the types are erased. You need a runtime check at the boundary, then the checked type flows through the rest of the code. The unknown and runtime data guide shows how.

Continue Learning TypeScript

  • Return to the TypeScript guide for the full language overview, including generics, tsconfig, and tooling.
  • Review the JavaScript guide if the runtime side of the language still feels shaky.
  • Still deciding whether types are worth it? JavaScript vs TypeScript walks the trade directly.

Sources

  1. [1]
    The Basics
    (typescriptlang.org)
  2. [2]
    Everyday Types
    (typescriptlang.org)
  3. [3]
    TSConfig Reference
    (typescriptlang.org)