Describing Object Shapes

Dee’s repair queue has a type now: every job waiting in it is a ticket number or a bike, (string | number)[]. Behind each ticket number, though, is a repair ticket, an object with the job’s details, and the checker doesn’t know its shape yet.

You’ve already seen how the checker writes down an object. When the AI-suggested line passed a whole ticket to laborCost(), the error described it as { bike: string; hours: number; }, a list of property names, each with a type.

In this lesson, you’ll write that kind of description yourself. You’ll give a repair ticket a shape, predict which tickets the checker accepts, and mark the properties that can be missing or mustn’t change. Start with the function that puts a ticket on the workshop board.

Describe a Ticket’s Properties

A function that reads properties from an object parameter needs to tell the checker which properties the object has. Dee’s workshop board shows one line per ticket: the bike and the hours the job needs. Create tickets.ts in your repair-queue project folder with the board’s code as it is in JavaScript:

Language: TypeScript Filename: tickets.ts
 1function boardLine(ticket) {
 2  return `${ticket.bike}: ${ticket.hours} h`;
 3}
 4
 5const amaraTicket = { bike: "Amara's commuter", hours: 1.5 };
 6console.log(boardLine(amaraTicket));

npm run check reports the error you know from laborCost(): Parameter 'ticket' implicitly has an 'any' type. A ticket is an object, and typeof says so in JavaScript, so a reasonable first try is the annotation ticket: object. Change line 1 to that, and run the check:

Language: Shell
$ npm run check

> [email protected] check
> tsc --noEmit

tickets.ts:2:20 - error TS2339: Property 'bike' does not exist on type 'object'.

2   return `${ticket.bike}: ${ticket.hours} h`;
                     ~~~~

tickets.ts:2:36 - error TS2339: Property 'hours' does not exist on type 'object'.

2   return `${ticket.bike}: ${ticket.hours} h`;
                                     ~~~~~

Found 2 errors in the same file, starting at: tickets.ts:2

The checker accepts the annotation, but now both property reads fail. The type object promises only that ticket is some object. It says nothing about the object’s properties, so the checker can’t let the function read .bike or .hours.

What the checker needs is the object’s shape. An object type lists each property and its type between braces, the same way the checker wrote the ticket in that earlier error. Change line 1:

Language: TypeScript Filename: tickets.ts
 1function boardLine(ticket: { bike: string; hours: number }) {
 2  return `${ticket.bike}: ${ticket.hours} h`;
 3}
 4
 5const amaraTicket = { bike: "Amara's commuter", hours: 1.5 };
 6console.log(boardLine(amaraTicket));

Run the check, then the file:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.