From Snakes 🐍 To Camels 🐫 - Create the CamelCase Type in TypeScript

Поделиться
HTML-код
  • Опубликовано: 19 окт 2024

Комментарии • 6

  • @wertig6925
    @wertig6925 3 месяца назад +2

    great video!

  • @batsshadow
    @batsshadow 2 месяца назад

    I'm not understanding how you can actually use this type in the real world. What can you do with this CamelCase type in a real application?

    • @mrlectus
      @mrlectus 2 месяца назад +1

      That's not the point

    • @batsshadow
      @batsshadow 2 месяца назад

      @@mrlectus so what is the point then? I understand it's demonstrating some power, but it needs a concrete application.
      I was actually trying to define a type where you could pass snake case to a function and not other cased strings. It is hard to do.
      This seems like maybe it's heading in that direction, but then it only appears to work by creating too specific of types.
      Help.

    • @awesome.rats159
      @awesome.rats159 Месяц назад

      @@batsshadow consider using the `never` type? i think you could say something like
      type SnakeOnly = T extends SnakeCase? T : never

  • @juanferrer5885
    @juanferrer5885 Месяц назад +1

    type snakeCase<
    T extends string,
    Acumulator extends string = ""
    > = T extends `${infer First}${infer Rest}`
    ? First extends Capitalize
    ? snakeCase
    : snakeCase
    : Acumulator;
    type camelCase = T extends `${infer Before}_${infer After}`
    ? camelCase
    : T;