ts-opaque-units
    TypeScript icon, indicating that this package has built-in type declarations

    0.1.1 • Public • Published

    ts-opaque-units

    minified minified + gzip tree-shaking support coverage

    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;
    
    /* Cannot assign different types */
    
    // @ts-expect-error
    hours = minutes;
    // @ts-expect-error
    minutes = hours;
    
    /** Can assign the same types */
    
    // ✅
    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");

    Install

    npm i ts-opaque-units

    DownloadsWeekly Downloads

    2

    Version

    0.1.1

    License

    MIT

    Unpacked Size

    22.1 kB

    Total Files

    66

    Last publish

    Collaborators

    • beraliv