Keep Milliseconds and Seconds Apart
Write the code, see it run, pass the checks.
The problem
- easy
- Code
Timeouts in milliseconds and in seconds are both plain numbers, so mixing them up is easy. Export branded types Ms and Seconds, constructors ms(n) and seconds(n), conversions toMs(s) and toSeconds(m), and addMs(a, b), which adds two durations in milliseconds. Type them so a plain number, or seconds, is rejected where Ms is expected (and the other way round), while a duration can still be read as a number.
Your code must pass
- Converts between seconds and milliseconds
- Adds durations in milliseconds
- Has no type errors
- Rejects a plain number as a duration
- Rejects seconds where milliseconds belong
- Durations stay distinct but read as numbers