Skip to content

Instantly share code, notes, and snippets.

@enpitsuLin
Last active May 18, 2022 00:53
Show Gist options
  • Select an option

  • Save enpitsuLin/30539766563938d7739ea94bb4b2cd03 to your computer and use it in GitHub Desktop.

Select an option

Save enpitsuLin/30539766563938d7739ea94bb4b2cd03 to your computer and use it in GitHub Desktop.
@enpitsuLin
Copy link
Author

//00730-hard-union-to-tuple
type StringToUnion<T extends string> = T extends `${infer F}${infer R}` ? F | StringToUnion<R> : never

尾递归和模板字符串

@enpitsuLin
Copy link
Author

enpitsuLin commented May 17, 2022

//02822-hard-split
type Split<S extends string, SEP extends string> = Equal<string, S> extends true
  ? string[]
  : S extends `${infer F}${SEP}${infer R}`
  ? R extends ''
    ? [F]
    : [F, ...Split<R, SEP>]
  : S extends ''
  ? SEP extends ''
    ? []
    : ['']
  : [S]

面向case穷举,好像也没看到更优雅的解法😂

@enpitsuLin
Copy link
Author

//00189-easy-awaited
type MyAwaited<X> = X extends Promise<infer T> ? T extends Promise<infer P> ? MyAwaited<T> : T : never

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment