Skip to content

Instantly share code, notes, and snippets.

@lbragile
Last active August 5, 2021 03:55
Show Gist options
  • Save lbragile/3176be88d6c374e4b69ab86659c03e7c to your computer and use it in GitHub Desktop.
Save lbragile/3176be88d6c374e4b69ab86659c03e7c to your computer and use it in GitHub Desktop.
A complete HEX string type using Template Literal Types
// https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html
type THexBase = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
type THexLower = "a" | "b" | "c" | "d" | "e" | "f";
type THexUpper = "A" | "B" | "C" | "D" | "E" | "F";
/**
* Includes 1 & 2 character hex strings.
*
* @example "0", "00", ..., "f", "ff", ..., "F", "FF"
*/
type THexStr =
| `${THexBase}${THexBase}`
| `${THexBase}${THexLower}`
| `${THexLower}${THexBase}`
| `${THexBase}${THexUpper}`
| `${THexUpper}${THexBase}`
| `${THexLower}${THexLower}`
| `${THexLower}${THexUpper}`
| `${THexUpper}${THexLower}`
| `${THexUpper}${THexUpper}`
| THexBase
| THexLower
| THexUpper;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment