Keep Two Kinds of ID from Mixing Up
Write the code, see it run, pass the checks.
The problem
- hard
- Code
- Pro
User ids look like u_ followed by digits and order ids like o_ followed by digits, but both are strings, so it's easy to pass one where the other belongs. Export branded types UserId and OrderId, and parsers userId(text) and orderId(text) that return the branded id or throw for text in the wrong format. Type them so that a function taking an OrderId rejects a plain string or a UserId (and the other way round), while an id can still be used as a string.
Your code must pass
- Parses ids in the right format
- Throws for the wrong format
- Has no type errors
- Parsers return branded ids
- Rejects a plain string as an id
- Rejects one kind of id for the other