Object Types & Aliases

The Course
•
5 min read

An object type describes required properties and their types. type Session = { topic: string; minutes: number } gives that shape a reusable name. A type alias is a name for a type expression. It does not construct a value or add runtime validation.

Compare Session with session. The capitalized name is a convention for a type; the lowercase name refers to actual data. Type names are removed when the program becomes JavaScript. You cannot print Session as though it were a value or test value instanceof Session.

A function accepting Session can read topic and minutes because the contract requires them. A missing field or a wrongly typed field is caught at a checked call site. A new object literal with a misspelled extra field often gets an excess-property diagnostic too. This is helpful typo detection, not a guarantee that objects can never have extra data.

TypeScript mainly compares object structure: whether the required fields are present with compatible types. A variable with extra fields can still be passed to a function needing only topic and minutes. You do not need to inherit from a special class. This lets small functions request just the data they use.

Use one clear alias for a repeated domain shape rather than copying an inline object annotation into several functions. If the domain changes, review the alias and the affected call sites together.

Predict, check, then run

Before running, write down the output and explain which information the checker knows. Click Check types first, then Check & run.

Predict, then runTypeScript
type Session = {
  topic: string;

  minutes: number;
};

function describe(session: Session): string {
  return `${session.topic}: ${session.minutes} min`;
}

const session = { topic: "Objects", minutes: 30, completed: true };

console.log(describe(session));

Enable JavaScript to edit and type-check this example.

Check your prediction
Objects: 30 min

Read a real compiler error

Read the spelling in the type and compare it with the value. Do not add another field to the type just to match a typo.

Find and fix the type errorTypeScript
type Session = {
  topic: string;

  minutes: number;
};

const session: Session = { topic: "Objects", minute: 30 };

console.log(session.minutes);

Enable JavaScript to edit and type-check this example.

Check your diagnosis

Use minutes in the object literal. The declared shape and the actual field must agree. A type alias cannot rename a property at runtime.

A smaller type does not remove extra fields

Object compatibility asks whether the required fields are available with compatible types. A record held in a variable can carry additional fields and still satisfy a smaller function parameter. That does not mean the runtime strips the extras when the function receives it.

A fresh object literal is checked more strictly for likely misspelled extra properties. Keep that authoring check separate from runtime filtering. If an output must contain only a public set of fields, construct those fields explicitly; an annotation alone cannot sanitize a record.

A smaller type does not remove extra fields: traceTypeScript
type PublicBook = { title: string };

const full = { title: "Types", internal: true };
const view: PublicBook = full;

console.log(view.title);
console.log(Object.keys(view).join(","));

Enable JavaScript to edit and type-check this example.

Check your prediction
Types
title,internal

The view is the same object under a smaller static contract. Reading view.internal is rejected through that type, but the property still exists in the runtime object.

A smaller type does not remove extra fields: contrastTypeScript
type PublicBook = { title: string };

const full = { title: "Types", internal: true };
const clean: PublicBook = { title: full.title };

console.log(Object.keys(clean).join(","));
console.log(clean === full);

Enable JavaScript to edit and type-check this example.

Check your prediction
title
false

The explicit object creates a new public value. Projection is behavior implemented by JavaScript, while the alias checks that the new value has the promised shape.

Try the rule on new inputs

Pass a variable with extra fields into a typed function, then try a fresh literal with a misspelled field. Read both outcomes carefully. Explain why hiding a field from type access is different from removing it from data.

Your turn

Exercise

Object Types & Aliases: practise

Define a Book type with title:string and pages:number. Write label(book) returning Title (N pages). Print Types (120 pages).

Your workspaceTypeScript
// Define Book and a label function. Create one book and print its label.

Enable JavaScript to edit and type-check this example.

Hint 1: the idea

Name the record shape once; annotate the function parameter with that name.

Hint 2: a first step

Begin the alias with type Book = and list the two required fields inside braces.

Show one solution
One possible solutionTypeScript
type Book = {
  title: string;

  pages: number;
};

function label(book: Book): string {
  return `${book.title} (${book.pages} pages)`;
}

console.log(label({ title: "Types", pages: 120 }));

Enable JavaScript to edit and type-check this example.

Types (120 pages)

Explain the types at the function boundary and the runtime checks, if any. Compare your approach before copying this answer.

Change it and explain it

Add a published field to a separate book variable and pass that variable to label. Explain why it works. Then remove pages and inspect the diagnostic. Which missing field would require a runtime check if the object came from JSON?

Was this article helpful?

Your response is saved on this device.