79172111

Date: 2024-11-09 04:34:56
Score: 1.5
Natty:
Report link

Turns out, there is a way to trick the compiler into doing what I want

type StartsWith<T extends string, P extends string> =
    T extends `${P}${infer R}`
        ? T extends `${infer _P extends P}${R}`
            ? `Prefix: ${_P}, Rest: ${R}`
            : never
        : never;

type Starts = '[[' | '<<';

type Z = StartsWith<"[[Text", Starts>;
//   ^?type z = "Prefix: [[, Rest: Text"

type Y = StartsWith<"<<ABC", Starts>;
//   ^?type Y = "Prefix: <<, Rest: ABC"
Reasons:
  • RegEx Blacklisted phrase (1): I want
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: thomasrea0113