Loading lesson…
type vs interface, optional fields, and structural typing. Model your data once and let every function benefit.
`interface` and `type` are almost interchangeable for object shapes. Interfaces are extendable and play nicely with classes. Type aliases handle unions, tuples, and mapped types.
interface User { id: string; email: string; name?: string; // optional readonly createdAt: Date; } interface AdminUser extends User { role: "admin"; } const u: AdminUser = { id: "u_1", email: "ada@example.com", createdAt: new Date(), role: "admin", }; // u.createdAt = new Date(); // error: readonlyOptional fields use `?`. Readonly prevents mutation. Extends builds richer types from simpler ones.type ID = string & { readonly __brand: "ID" }; // branded nominal type type Result<T> = | { ok: true; value: T } | { ok: false; error: string }; function parse(input: string): Result<number> { const n = Number(input); return Number.isNaN(n) ? { ok: false, error: "not a number" } : { ok: true, value: n }; } const r = parse("42"); if (r.ok) console.log(r.value * 2); else console.error(r.error);Discriminated unions force you to check the tag before using the payload. Makes null checks compile-time.The big idea: model your domain with interfaces and unions. Every function you write afterward gets safety for free.
6 questions · take it digitally for instant feedback at tendril.neural-forge.io/learn/quiz/end-progx-ts-interfaces-creators
What is the main idea of "TypeScript Types and Interfaces"?
Which concept is most central to "TypeScript Types and Interfaces"?
What should a careful learner remember about "Rule of thumb"?
You want to use AI after this lesson. What is the safest next step?
How should AI output about interface be treated?
Name one way to verify an AI answer about interface.