Skip to content

Instantly share code, notes, and snippets.

@ljbc1994
Last active May 7, 2022 15:37
Show Gist options
  • Save ljbc1994/8d51477b702770e56231bacb4568b9b3 to your computer and use it in GitHub Desktop.
Save ljbc1994/8d51477b702770e56231bacb4568b9b3 to your computer and use it in GitHub Desktop.
Random Types!
type CensorWithChar<TPhrase extends string, TChar extends string, TResult extends string = ''> =
TPhrase extends `${infer Head}${infer Tail}`
? CensorWithChar<Tail, TChar, `${TResult}${Head extends TChar ? '*' : Head}`>
: TResult
type CensorWithCharTest = CensorWithChar<'my name is mary celeste', 'm' | 'c'>
// type CensorWithCharTest = "*y na*e is *ary *eleste"
type Reverse<TPhrase extends string, TResult extends string = ''> =
TPhrase extends `${infer Head}${infer Tail}`
? Reverse<Tail, `${Head}${TResult}`>
: TResult
type ReverseTest = CensorWithChar<'my name is mary celeste'>
// type ReverseTest = "etselec yram si eman ym"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment