• DrM@feddit.de
    link
    fedilink
    arrow-up
    44
    arrow-down
    1
    ·
    11 months ago

    I hate Typescript for promising me that nobody can put cyanide on the list, but in reality it disallows ME from putting cyanide on the list, but everyone else from the outside is still allowed to do so by using the API which is plain JavaScript again

      • DrM@feddit.de
        link
        fedilink
        arrow-up
        13
        ·
        edit-2
        11 months ago

        The main problem with JavaScript and TypeScript is that there is such a little entrybarrier to it, that way too many people use it without understanding it. The amount of times that we had major issues in production because someone doesn’t understand TypeScript is not countable anymore and our project went live only 4 months ago.

        For example, when you use nest.js and want to use a boolean value as a query parameter.

        As an example:

        @Get('valueOfMyBoolean')
        @ApiQuery(
          {
            name: 'myBoolean',
            type: boolean,
          }
        )
        myBooleanFunction(
          @Query('myBoolean') myBoolean: boolean
        ){
          if(myBoolean){
            return 'myBoolean is true';
          }
          return 'myBoolean is false';
        }
        

        You see this code. You don’t see anything wrong with it. The architect looks at it in code review and doesn’t see anything wrong with it. But then you do a GET https://something.com/valueOfMyBoolean?myBoolean=false and you get “myBoolean is true” and if you do typeOf(myBoolean) you will see that, despite you declaring it twice, myBoolean is not a boolean but a string. But when running the unit-tests, myBoolean is a boolean.

        • shastaxc@lemm.ee
          link
          fedilink
          arrow-up
          6
          ·
          11 months ago

          This is more a condemnation of nest.js than ts. It seems great in theory. I like the architecture and the ability to share models and interfaces between front and backend, but it’s objectively makes everything more complicated. It adds layers of abstraction that should not be necessary and it’s such a niche/unpopular framework for backend systems that you generally have to jump through hoops to do anything moderately complex. Not only do new devs have to learn typescript to use it, they have to learn the nest architecture to know how to do things “the right way” and you still end up in situations like this which looks perfectly valid but isn’t. Typescript was never meant to be used for backend, and trying to make it do so and then complaining about it is like jogging while carrying a gun, shooting yourself in the foot, and blaming the gun.

        • CanadaPlus@lemmy.sdf.org
          link
          fedilink
          arrow-up
          5
          ·
          11 months ago

          I’ve never used TS, and I’m not exactly sure what nest.js even does, but building a TypeScript project on top of a JavaScript library not designed for it seems like asking for trouble. Is that standard practice?

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

          Typically when creating API interfaces you’d be better off marking the inputs as unknown, and then using something like Zod to validate the types

        • uis@lemmy.world
          link
          fedilink
          arrow-up
          2
          arrow-down
          2
          ·
          11 months ago

          return ‘myBoolean is true’;

          I instantly noticed this line. Shitcode is so fun.