这是indexloc提供的服务,不要输入任何密码
Skip to content

545 - printf (🎥 Video Explanation and Solution) #25958

@MichiganTypeScript

Description

@MichiganTypeScript

545 - printf

If you've been following along with the challenges, you should start to feel pretty empowered by a challenge like printf because it gives you a chance to create quite dynamic outputs from a simple string input. It's powerful stuff!

🎥 Video Explanation

Release Date: 2023-04-13 19:00 UTC

printf

🔢 Code

// ============= Test Cases =============
import type { Equal, Expect } from './test-utils'

type A1 = Format<'abc'>;
type B1 = string;
type C1 = Expect<Equal<A1, B1>>;

type A2 = Format<'a%sbc'>;
type B2 = (s1: string) => string;
type C2 = Expect<Equal<A2, B2>>;

type A3 = Format<'a%dbc'>;
type B3 = (d1: number) => string;
type C3 = Expect<Equal<A3, B3>>;

type A4 = Format<'a%%dbc'>;
type B4 = string;
type C4 = Expect<Equal<A4, B4>>;

type A5 = Format<'a%%%dbc'>;
type B5 = (d1: number) => string;
type C5 = Expect<Equal<A5, B5>>;

type A6 = Format<'a%dbc%s'>;
type B6 = (d1: number) => (s1: string) => string;
type C6 = Expect<Equal<A6, B6>>;

// ============= Your Code Here =============

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

type Format<T extends string> =
  T extends `${string}%%${infer Control extends string}`
  ? Format<Control>
  : T extends `${
      string
    }%${
      infer Control extends keyof FormatTypes
    }${
      infer Tail extends string
    }`
    ? (...args: FormatTypes[Control]) => Format<Tail>
    : string

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

type Format<T extends string> = 
  T extends `${
    string
  }%${
    infer Control extends keyof FormatTypes | '%'
  }${
    infer Tail extends string
  }`
  ? Control extends keyof FormatTypes
    ? FormatTypes<Format<Tail>>[Control]
    : Format<Tail>
  : string

interface Dictionary {
  's': string,
  'd': number
};

// @justBadProgrammer
type Trim<S extends string > =
  S extends `${infer R}%%${infer U}`
  ? Trim<`${R}${U}`>
  : S;

type Format<
  T extends string,
> = 
  Trim<T> extends`${
    string
  }%${
    infer R extends keyof Dictionary
  }${
    infer U
  }`
  ? (arg: Dictionary[R]) => Format<U>
  : string;


// @humandetail
type M = {
  d: number;
  s: string;
}

type Format<
  T extends string,
  Acc extends string = ''
> =
  T extends `${infer Head}${infer Tail}`
  ? Acc extends '%'
    ? Head extends '%'
      ? Format<Tail, ''>
      : Head extends keyof M
        ? (arg: M[Head]) => Format<Tail, ''>
        : Format<Tail, Head>
    : Format<Tail, Head>
  : string

➕ More Solutions

For more video solutions to other challenges: see the umbrella list! #21338

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions