I keep seeing posts of some drama with typescript. What happened?

  • Cratermaker@discuss.tchncs.de
    link
    fedilink
    arrow-up
    4
    arrow-down
    1
    ·
    1 year ago

    I’ve heard it’s more of a problem on the library side. But I’ve personally had pains with ts when working with quirky features such as enums or discriminating unions. Part of the problem in my opinion is that the types all disappear at runtime, so you lose a lot of the joy of a statically typed language. For example, an API can pass you unexpected garbage and all your ts type wrangling helps not at all.

    • FooBarrington@lemmy.world
      link
      fedilink
      arrow-up
      2
      ·
      1 year ago

      Well yes, if you don’t take care of properly typing external data, you won’t have it properly recognised. But that’s the same in any language that e.g. consumes external JSON data. Use the tools that Typescript gives you (like type guards) or the tools the community has built (like io-ts).

    • jpeps@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      1 year ago

      For your API issue, have you tried using type guards or something more sophisticated like Zod?