Skip to content

Instantly share code, notes, and snippets.

@vlastachu
Created December 14, 2024 13:46
Show Gist options
  • Save vlastachu/13ee41a2b1727252d719781713fee14e to your computer and use it in GitHub Desktop.
Save vlastachu/13ee41a2b1727252d719781713fee14e to your computer and use it in GitHub Desktop.
ts-advent-2024-12.ts
type StrToNum<S extends string> =
S extends `${infer I extends number}` ? I : never;
type NaughtyOrNice<S extends string, Result = 'naughty'> =
S extends "" ? Result :
S extends `${infer _}${infer R}`
? NaughtyOrNice<R, Result extends 'naughty' ? 'nice' : 'naughty'>
: never;
type FormatNames<A extends string[][]> =
{ [K in keyof A]:
{ name: A[K][0],
count: StrToNum<A[K][2]>,
rating: NaughtyOrNice<A[K][0]>
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment