ts-opaque-units

Small TypeScript opaque measurement library
Install
yarn add ts-opaque-units typescript@^4.0.0
Opaque types
For now, only time units are available:
Milliseconds
Seconds
Minutes
Hours
Days
Weeks
Months
Years
Decades
Centuries
Millenniums
You are not allowed to assign variables of different types, so you are safe ✅
let hours = 1 as Hours;
let twoHours = 2 as Hours;
let minutes = 1 as Minutes;
let twoMinutes = 2 as Minutes;
hours = minutes;
minutes = hours;
minutes = twoMinutes;
hours = twoHours;
Convert
To transform one type to another safely, use a function convert
:
import { convert, Minutes, Years } from "ts-opaque-units";
const minutesInHour = 60 as Minutes;
const secondsInHour = convert(minutesInHour, "minutes", "seconds");
const year = 1 as Years;
const secondsInYear = convert(year, "years", "seconds");