Skip to content

Instantly share code, notes, and snippets.

@tonyonodi
Created August 7, 2021 11:42
Show Gist options
  • Save tonyonodi/656ffa655e7208772e368e25e9700ba7 to your computer and use it in GitHub Desktop.
Save tonyonodi/656ffa655e7208772e368e25e9700ba7 to your computer and use it in GitHub Desktop.
Not very useful implementation of Char in TypeScript
const chartag = Symbol("char");
export type Char = string & { __tag: typeof chartag };
type IsChar<S extends string> = S extends `${infer Head}${infer Tail}`
? Head extends ""
? never
: Tail extends ""
? Char
: never
: never;
type C = IsChar<"c">; // Char
type CC = IsChar<"cc">; // never
type EC = IsChar<"">; // never
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment