79172240

Date: 2024-11-09 06:48:25
Score: 0.5
Natty:
Report link

when you use infer with a constraint that is a union type containing multi character strings TypeScript seems to prioritize character by character matching rather than trying to match the complete union members

here is how we can do workaround by checking if the string starts with any of the union members :)

type StartsWith<T extends string, U extends string> = T extends `${infer P}${string}`
  ? P extends U
    ? P
    : never
  : never;


type Z = GetPrefix<'[[Text', '[[' | '<<'>; 
// "[["
Reasons:
  • Has code block (-0.5):
  • Starts with a question (0.5): when you use in
  • Low reputation (0.5):
Posted by: Milan Sachani