TypeScript: Branded ids for users and orders

User ids and order ids are both strings, and mixing them up is an easy bug. Brand them so the compiler keeps them apart.

What to build

A program that records users and orders and answers questions about them.

Requirements

  • Declare branded types UserId (text U followed by digits) and OrderId (text O followed by digits), each created only by a parse function returning the id or null.
  • Commands: user ID NAME adds a user; order ORDERID USERID records an order for an existing user; owner ORDERID prints ORDERID belongs to NAME; orders USERID prints NAME: ORDERID, ... in the order placed, or NAME: no orders.
  • A malformed id prints bad id TEXT (check ids in the order they appear in the command); an unknown user or order prints unknown TEXT.

Input

Line 1: n. Then n commands.

Output

One line per owner and orders command, and per error.

Constraints

1 ≤ n ≤ 100; names are single words.

Example

Input:

6
user U1 Ada
user U2 Alan
order O10 U1
order O11 U1
owner O11
orders U2

Output:

O11 belongs to Ada
Alan: no orders

Why it works

Because a UserId and an OrderId are different types, passing an order id where a user id is wanted is a compile error, and since only the parse functions create them, every id in the maps has been checked.

Examples

Example 1 · Input
6
user U1 Ada
user U2 Alan
order O10 U1
order O11 U1
owner O11
orders U2
Expected output
O11 belongs to Ada
Alan: no orders
Example 2 · Input
4
user U1 Ada
order U1 O5
order O5 U9
owner O77
Expected output
bad id U1
unknown U9
unknown O77

Hints

Hint 1

type UserId = string & { readonly __brand: "UserId" } and the same for OrderId, each made only by its parse function.

Hint 2

Keep orders in a Map<OrderId, UserId> and users in a Map<UserId, string>.

Solution

Show one correct solution (try it yourself first)
const read = (): string => prompt() ?? "";

type UserId = string & { readonly __brand: "UserId" };
type OrderId = string & { readonly __brand: "OrderId" };

function parseUserId(text: string): UserId | null {
  return /^U\d+$/.test(text) ? (text as UserId) : null;
}

function parseOrderId(text: string): OrderId | null {
  return /^O\d+$/.test(text) ? (text as OrderId) : null;
}

const users = new Map<UserId, string>();
const owners = new Map<OrderId, UserId>();
const placed = new Map<UserId, OrderId[]>();

const n = Number(read());
for (let i = 0; i < n; i++) {
  const [command, a, b] = read().split(" ");
  if (command === "user") {
    const id = parseUserId(a);
    if (!id) console.log("bad id " + a);
    else {
      users.set(id, b);
      if (!placed.has(id)) placed.set(id, []);
    }
  } else if (command === "order") {
    const orderId = parseOrderId(a);
    if (!orderId) {
      console.log("bad id " + a);
      continue;
    }
    const userId = parseUserId(b);
    if (!userId) console.log("bad id " + b);
    else if (!users.has(userId)) console.log("unknown " + b);
    else {
      owners.set(orderId, userId);
      placed.get(userId)?.push(orderId);
    }
  } else if (command === "owner") {
    const orderId = parseOrderId(a);
    const userId = orderId ? owners.get(orderId) : undefined;
    if (!orderId) console.log("bad id " + a);
    else if (!userId) console.log("unknown " + a);
    else console.log(orderId + " belongs to " + users.get(userId));
  } else {
    const userId = parseUserId(a);
    if (!userId) console.log("bad id " + a);
    else if (!users.has(userId)) console.log("unknown " + a);
    else {
      const list = placed.get(userId) ?? [];
      console.log(users.get(userId) + ": " + (list.length ? list.join(", ") : "no orders"));
    }
  }
}

Lesson: Designing type-safe programs →