TypeScript for real code · Chapter 10 of 36

Who is this?

By the end of this chapter you can tell objects of different classes apart, and write your own checks that TypeScript understands: type predicates and assertion functions. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Modelling Tapri
  • 4 tasks
  • TypeScript
  • In the all labs plan

The first 2 chapters of TypeScript for real code are free. This one comes with the all labs plan.

Why you need this

The home screen greets everyone the same way, so Raju the rider was just told his chai is on the way. Anjali wants riders to see their bike and customers their chai. Ravi wants a list of only the riders on shift, and a check that stops the dispatch code when a customer slips into it.

Riders and customers are two classes, and both have a name, so that is all TypeScript lets you read until you check. The checks you know so far work inside one if. Move a check into a helper function that returns a boolean, and TypeScript forgets what it proved: the helper says "true or false", not what true means. Bunty's way out is a cast, p as Rider, which checks nothing.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    greet: riders and customers, told apart with instanceof

    The home screen greets riders and customers differently. greet(new Rider('Raju', 'KA01 4521')) is 'Raju, your bike KA01 4521 is loaded', and greet(new Customer('Anjali', '98450 12345')) is 'Hi Anjali, your chai is on the way'.

    Keep the parameter a Person, so greet('Raju') is a type error, and the return type a string. Tell the two apart with instanceof, so TypeScript knows which one it has.

  2. Task 2

    isRider: a type predicate

    isRider already works when the file runs: isRider(new Rider('Salma', 'KA05 0099')) is true and isRider(new Customer('Meera', '98860 00000')) is false. But its return type says only boolean, so an if around it narrows nothing.

    Change the return type to a type predicate, so that inside if (isRider(p)) p is a Rider, and in the else branch p is a Customer. Leave the body as it is.

  3. Task 3 · interview question

    Interview: ridersOnly, only riders, in the type too

    ridersOnly picks the riders out of a mixed list, keeping their order: for [raju, anjali, salma] it is [raju, salma], and for [anjali] it is [].

    Its return type must be exactly Rider[], not Person[], with no cast (as). The parameter stays a Person[], so a list of names like ['Raju', 'Salma'] is a type error. Use filter with isRider. Dev will ask why filter with a helper that returns boolean still gives Person[], and how to fix it without a cast.

  4. Task 4

    assertRider: an assertion function

    assertRider stops the dispatch code when it gets a customer. For a customer it throws an Error whose message is the name followed by ' is not a rider': assertRider(new Customer('Anjali', '98450 12345')) throws 'Anjali is not a rider'. For a rider it does nothing.

    Give it the return type asserts person is Rider instead of void, so that after a call to assertRider(p), p is a Rider.

What you will learn

  • instanceof: asks which class made an object, and narrows to that class.
  • Type predicates: a return type like table is string, so a helper's true narrows at the call, and filter can narrow a whole list.
  • Assertion functions: asserts stock is number, a function that throws when the check fails and narrows after the call.

More in Modelling Tapri

Build "Who is this?" yourself

Tapri’s codebase grows, and types keep it honest. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of TypeScript for real code are free. This one comes with the all labs plan.