Last active
May 7, 2022 15:37
-
-
Save ljbc1994/8d51477b702770e56231bacb4568b9b3 to your computer and use it in GitHub Desktop.
Random Types!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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