Typed Objects
A typed object is a value described by an object type: a list of properties, each with a type, optionality, and maybe readonly. Assignability is structural. Fresh literals get extra excess-property checks.
Detailed Explanation
Most TypeScript programs are about objects. An object type is the set of values that have at least the listed members with compatible types.
type User = {
id: string;
email: string;
displayName?: string;
};
A value with those properties, and possibly more, is a User when it arrives through a variable. A fresh object literal assigned directly to User may not have extra properties. That extra check exists because a typo (dislayName) would otherwise silently create a new property and leave displayName undefined. After the value is stored in a wider variable, the extra property is allowed and forgotten.
This is structural typing in everyday form. There is no new User required. Two type aliases with the same members are interchangeable. An interface and a type alias with the same members are interchangeable. A class instance with those public fields is interchangeable.
Index signatures and Record widen the set of keys. Intersection (A & B) combines members. Union (A | B) requires you to narrow before using unshared keys. readonly on a property blocks assignment through that type. Optional ? means the property may be omitted; under exactOptionalPropertyTypes, present-and-undefined is not the same as omitted.
Object types can describe function properties, getters (as a readonly property in many cases), and call/construct signatures when written as interfaces. They do not describe private class fields unless those fields are part of a class type.
The practical design rule: name the object types at module boundaries. Inline { id: string } in one function is fine. Inline { id: string; email: string; role: Role; createdAt: Date } repeated six times will drift. Use interface if you expect merging or extends. Use type if you will compute the shape.
At runtime the object is a JavaScript object. The type is erased. Extra properties are still there. Missing properties are undefined. JSON.parse will not validate the type you annotate. Typed objects are a compile-time claim about shape, and they need a runtime check at the trust boundary.
Code Examples
1. Structural assignability, and excess-property checking on literals.
// 1. Structural assignability, and excess-property checking on literals.
type User = { id: string; email: string };
const ada: User = { id: "u1", email: "ada@example.com" };
const extra = { id: "u2", email: "grace@example.com", role: "admin" };
const ok: User = extra; // fine — extra came through a variable
const bad: User = { id: "u3", email: "x@y.z", role: "admin" };
// Error: 'role' does not exist in type 'User' (excess property).2. Optional vs undefined.
// 2. Optional vs undefined.
type Profile = { nickname?: string };
function label(profile: Profile): string {
return profile.nickname ?? "none";
}
label({});
label({ nickname: "Ada" });3. A closed public DTO vs an open bag.
// 3. A closed public DTO vs an open bag.
type PublicUser = { id: string; email: string };
type Bag = Record<string, unknown>;
function toPublic(bag: Bag): PublicUser | undefined {
if (typeof bag.id === "string" && typeof bag.email === "string") {
return { id: bag.id, email: bag.email };
}
return undefined;
}Common Gotchas
Excess-property checks apply to fresh literals only
a typo on an intermediate object will not be caught. Annotate the intermediate, or use `satisfies`.
Optional `?` is not the same as `| undefined` on a required key
see `exactOptionalPropertyTypes` if you need that distinction enforced.
Extra runtime properties survive
`JSON.stringify` will print them even if the type omitted them. Strip at the boundary if that matters.
Annotating `JSON.parse` as a typed object is not validation
the claim is unchecked. Use a type guard or a schema.