Created
May 10, 2023 06:54
-
-
Save cbebe/504e5098d3674da574ff85e8126cdb22 to your computer and use it in GitHub Desktop.
TimeInterval nominal type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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