TypeScript for real code · Chapter 25 of 36

Many signatures

By the end of this chapter you can give one function several call signatures, know when a union is the better choice, and type this in functions and in method chains. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Types that compute types
  • 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

Anjali is building the receipt screen. format(40) gives '₹40' and format([40, 25]) gives a list, but every caller gets back string | string[] and has to check which one it got: "I gave it a number. Why does it think I might get a list?" Bunty has gone the other way and written overloads for everything, and now the order form no longer compiles. A shared rider function reads this, which TypeScript types as any, and a gift cart forgets it is a gift cart halfway through a chain.

With one signature, the return type is the same for every call, so a function whose answer depends on its input returns a union and every caller narrows it again.

What you build: 4 tasks

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

  1. Task 1

    format: a price gives text, a list gives a list

    format(40) is '₹40' and format([40, 25]) is ['₹40', '₹25'], but every caller gets string | string[] back and has to check which. Add two overload signatures above the implementation, so format(40) has the type string and format([40, 25]) has the type string[].

    Keep the implementation as it is. Text like '40' must still be a type error.

  2. Task 2 · interview question

    Interview: normaliseItems, one signature instead of overloads

    The order form sends either text, like ' masala chai, bun maska ,', or a list of names, and readForm() returns string | string[] because it may be either. Bunty wrote one overload per kind, so normaliseItems(readForm(...)) is a type error.

    Replace his three lines with one signature, normaliseItems(input: string | string[]): string[], so text, a list, or a value that may be either all give string[]. A number is still a type error. normaliseItems(' masala chai, bun maska ,') is ['masala chai', 'bun maska'] and normaliseItems([' kulhad ', '']) is ['kulhad']. Dev will ask when one signature with a union is better than overloads.

  3. Task 3

    describeRider: say what this must be

    describeRider is shared by rider objects as their describe method, and reads this. Under strict settings TypeScript will not guess what this is. Give it a first parameter named this, of type Rider (not any), so calling describeRider() on its own is a type error, and so is calling it on an object with no zone.

    ravi.describe() is 'Ravi rides in Indiranagar', and describeRider.call({ name: 'Kiran', zone: 'HSR' }) is 'Kiran rides in HSR'.

  4. Task 4

    Cart: methods that return this, so GiftCart can chain

    add() returns the cart so calls can chain, but its return type says Cart, so a GiftCart forgets it is a GiftCart after add(), and withMessage is a type error. Make add and withMessage return the type this instead.

    new GiftCart().add('masala chai').withMessage('Happy Diwali').add('bun maska') has the type GiftCart, its items are ['masala chai', 'bun maska'] and its message is 'Happy Diwali'. new Cart().add('cutting chai') still has the type Cart, and a plain Cart has no withMessage.

What you will learn

  • Overloads: several call signatures above one implementation, so the return type follows the arguments.
  • When a union is better: one signature that takes a union, when the answer's type does not depend on the input.
  • this: a this parameter for a function used as a method, and the this type for methods that return their own object.

More in Types that compute types

Build "Many signatures" 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.