Skip to content

Instantly share code, notes, and snippets.

@cbebe
Created May 10, 2023 06:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbebe/504e5098d3674da574ff85e8126cdb22 to your computer and use it in GitHub Desktop.
Save cbebe/504e5098d3674da574ff85e8126cdb22 to your computer and use it in GitHub Desktop.
TimeInterval nominal type
/**
* TimeInterval nominal type and converters
*/
declare const TypeSymbol: unique symbol;
export type TimeInterval = number & { [TypeSymbol]: 'TimeInterval' };
/* Functions for converting to TimeInterval */
export const milliseconds = (howMany: number) => howMany as TimeInterval;
export const seconds = (howMany: number) => (howMany * 1000) as TimeInterval;
export const minutes = (howMany: number) => (howMany * 1000 * 60) as TimeInterval;
export const hours = (howMany: number) => (howMany * 1000 * 3600) as TimeInterval;
export const days = (howMany: number) => (howMany * 1000 * 3600 * 24) as TimeInterval;
export const weeks = (howMany: number) => (howMany * 1000 * 3600 * 24 * 7) as TimeInterval;
/* Functions for converting to numbers */
export const toSeconds = (timeInterval: TimeInterval) => timeInterval / 1000;
export const toMilliseconds = (timeInterval: TimeInterval) => timeInterval as number;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment