Skip to content
Typeutilsv0.10.0

ValidDuration

A duration string that parseDuration accepts, like '30m', '24h', or '500ms'. It's a whole number above 0, followed by one of the units ms, s, m, h, or d.

Use it to make your own function reject a bad duration at compile time. A call like every('1.5h') then fails to compile, and the error explains what a duration looks like. A value typed as string, like one from .env, still compiles. Check it with parseDuration at run time.

type ValidDuration<TText extends string> =
    string extends TText ? TText : EachDuration<TText>;
declare function every<const TText extends string>(duration: ValidDuration<TText>): void;

every('90s');
every('1.5h'); // fails to compile

TText

The literal to check.

TText extends string