Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

545 - printf (with original argument names) #23835

Open
BOCbMOU opened this issue Feb 18, 2023 · 1 comment
Open

545 - printf (with original argument names) #23835

BOCbMOU opened this issue Feb 18, 2023 · 1 comment
Labels
545 answer Share answers/solutions to a question en in English

Comments

@BOCbMOU
Copy link

BOCbMOU commented Feb 18, 2023

type FormatTypes = {
  s: [s1: string];
  d: [d1: number];
}

type Format<T extends string> = T extends `${string}%%${infer TRest extends string}` ? Format<TRest> : (
  T extends `${string}%${infer F extends keyof FormatTypes}${infer TRest extends string}` ? (
    (...args: FormatTypes[F]) => Format<TRest>
  ) : string
)
type T = Format<'a%dbc%s'> // type T = (d1: number) => (s1: string) => string 
@BOCbMOU BOCbMOU added answer Share answers/solutions to a question en in English labels Feb 18, 2023
@github-actions github-actions bot added the 545 label Feb 18, 2023
@BOCbMOU
Copy link
Author

BOCbMOU commented Feb 18, 2023

One more solution:

type FormatTypes<T = never> = {
  s: (s1: string) => T;
  d: (d1: number) => T;
}

type Format<T extends string> = 
  T extends `${string}%${infer F extends keyof FormatTypes | '%'}${infer TRest extends string}` ? (
    F extends keyof FormatTypes ? (
      FormatTypes<Format<TRest>>[F]
    ) : Format<TRest>
  ) : string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
545 answer Share answers/solutions to a question en in English
Projects
None yet
Development

No branches or pull requests

1 participant