Skip to content

Instantly share code, notes, and snippets.

@nghiepdev
Last active June 4, 2023 08:21
Show Gist options
  • Save nghiepdev/b9e85bc96a0a83b026960b5b12351f4c to your computer and use it in GitHub Desktop.
Save nghiepdev/b9e85bc96a0a83b026960b5b12351f4c to your computer and use it in GitHub Desktop.
Typing for TimeString and DateString
type Digit = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
type H = `${0 | 1}${Digit}` | `${2}${0 | 1 | 2 | 3}`;
type M = `${0 | 1 | 2 | 3 | 4 | 5}${Digit}`;
type S = `${0 | 1 | 2 | 3 | 4 | 5}${Digit}`;
export type TimeString = `${H}:${M}` | `${H}:${M}:${S}`; // 03:45 or 03:45:56
type Mo = Exclude<`0${Digit}` | `1${0 | 1 | 2}`, '00'>;
type Da = Exclude<`${0 | 1 | 2}${Digit}` | `3${0 | 1}`, '00'>;
export type DateString = `${number}-${Mo}-${Da}`; // 2017-02-23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment