11export type Nano = number & { readonly _brand : "nano" } ;
22
3- export const Nano = {
3+ // Calling `Nano(x)` brands a raw number as nanoseconds. The unit is the caller's assertion: no
4+ // conversion happens, so reach for `fromMicro`/`fromMilli`/`fromSecond` when the source has a unit.
5+ export const Nano = Object . assign ( ( value : number ) : Nano => value as Nano , {
46 zero : 0 as Nano ,
57 fromMicro : ( us : Micro ) : Nano => ( us * 1_000 ) as Nano ,
68 fromMilli : ( ms : Milli ) : Nano => ( ms * 1_000_000 ) as Nano ,
@@ -15,11 +17,13 @@ export const Nano = {
1517 div : ( a : Nano , b : number ) : Nano => ( a / b ) as Nano ,
1618 max : ( a : Nano , b : Nano ) : Nano => Math . max ( a , b ) as Nano ,
1719 min : ( a : Nano , b : Nano ) : Nano => Math . min ( a , b ) as Nano ,
18- } as const ;
20+ } ) ;
1921
2022export type Micro = number & { readonly _brand : "micro" } ;
2123
22- export const Micro = {
24+ // Calling `Micro(x)` brands a raw number as microseconds. See the `Nano` note: this asserts the unit
25+ // rather than converting, so use `fromNano`/`fromMilli`/`fromSecond` to convert from another unit.
26+ export const Micro = Object . assign ( ( value : number ) : Micro => value as Micro , {
2327 zero : 0 as Micro ,
2428 fromNano : ( ns : Nano ) : Micro => ( ns / 1_000 ) as Micro ,
2529 fromMilli : ( ms : Milli ) : Micro => ( ms * 1_000 ) as Micro ,
@@ -34,11 +38,13 @@ export const Micro = {
3438 div : ( a : Micro , b : number ) : Micro => ( a / b ) as Micro ,
3539 max : ( a : Micro , b : Micro ) : Micro => Math . max ( a , b ) as Micro ,
3640 min : ( a : Micro , b : Micro ) : Micro => Math . min ( a , b ) as Micro ,
37- } as const ;
41+ } ) ;
3842
3943export type Milli = number & { readonly _brand : "milli" } ;
4044
41- export const Milli = {
45+ // Calling `Milli(x)` brands a raw number as milliseconds. See the `Nano` note: this asserts the unit
46+ // rather than converting, so use `fromNano`/`fromMicro`/`fromSecond` to convert from another unit.
47+ export const Milli = Object . assign ( ( value : number ) : Milli => value as Milli , {
4248 zero : 0 as Milli ,
4349 fromNano : ( ns : Nano ) : Milli => ( ns / 1_000_000 ) as Milli ,
4450 fromMicro : ( us : Micro ) : Milli => ( us / 1_000 ) as Milli ,
@@ -53,11 +59,13 @@ export const Milli = {
5359 div : ( a : Milli , b : number ) : Milli => ( a / b ) as Milli ,
5460 max : ( a : Milli , b : Milli ) : Milli => Math . max ( a , b ) as Milli ,
5561 min : ( a : Milli , b : Milli ) : Milli => Math . min ( a , b ) as Milli ,
56- } as const ;
62+ } ) ;
5763
5864export type Second = number & { readonly _brand : "second" } ;
5965
60- export const Second = {
66+ // Calling `Second(x)` brands a raw number as seconds. See the `Nano` note: this asserts the unit
67+ // rather than converting, so use `fromNano`/`fromMicro`/`fromMilli` to convert from another unit.
68+ export const Second = Object . assign ( ( value : number ) : Second => value as Second , {
6169 zero : 0 as Second ,
6270 fromNano : ( ns : Nano ) : Second => ( ns / 1_000_000_000 ) as Second ,
6371 fromMicro : ( us : Micro ) : Second => ( us / 1_000_000 ) as Second ,
@@ -72,4 +80,4 @@ export const Second = {
7280 div : ( a : Second , b : number ) : Second => ( a / b ) as Second ,
7381 max : ( a : Second , b : Second ) : Second => Math . max ( a , b ) as Second ,
7482 min : ( a : Second , b : Second ) : Second => Math . min ( a , b ) as Second ,
75- } as const ;
83+ } ) ;
0 commit comments