This solution is generated by ChatGPT, correctness is not guaranteed. You might also be able to find a solution fromcommunity posts.
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 stringT 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
- ifL
exists (input string is not empty), return it, otherwise returnnever
(as per the question requirements).