This solution is generated by ChatGPT, correctness is not guaranteed.

AI solution for TypeScript puzzle on BFE.dev
17. implement LastChar<T>

type LastChar<T extends string> = T extends `${infer _}${infer L}` ? L : never;

Explanation:

  • T extends string - make sure the input type is a string
  • T extends ${infer _}${infer L}`` - match input string to a template literal with two parts, where the first part is any number of characters (we don't care about those characters, thus using _ to discard them) and the second part is the actual last character (which we want to extract)
  • L - this is the inferred last character from the input string
  • ? L : never - if L exists (input string is not empty), return it, otherwise return never (as per the question requirements).