What does the following type-level string manipulation compute? type CamelToSnake = S extends `${infer Head}${infer Tail}` ? Head extends Uppercase ? `_${Lowercase}${CamelToSnake}` : `${Head}${CamelToSnake}` : S;

TypeScript Professional Hard

TypeScript Professional — Hard

What does the following type-level string manipulation compute? type CamelToSnake = S extends `${infer Head}${infer Tail}` ? Head extends Uppercase ? `_${Lowercase}${CamelToSnake}` : `${Head}${CamelToSnake}` : S;

Key points

  • Conditional type checking is used to determine if the current character is uppercase or lowercase.
  • Recursion is applied to process each character in the string.
  • The resulting string has underscores inserted before uppercase letters to convert camelCase to snake_case.

Ready to go further?

Related questions