• capybara@lemm.ee
    link
    fedilink
    English
    arrow-up
    31
    arrow-down
    4
    ·
    2 months ago

    To start off… Using arithmetic operators on strings in combination with integers is a pure skill issue. Let’s disregard this.

    If you were to use + where one part is a string, it’s natural to assume a string appending is desired since + is commonly used as a function for this. On the other hand, - is never used for any string operation. Therefore, it’s safe to assume that it relates to actual artihmetics and any strings should therefore be converted to numerical values.

    This is an issue with untyped languages. If you don’t like it, use typescript. End of story.

    • Jankatarch@lemmy.world
      link
      fedilink
      arrow-up
      26
      arrow-down
      3
      ·
      2 months ago

      Instead of trying to make it work, javascript could just say “error.” Being untyped doesn’t mean you can’t have error messages.

      • capybara@lemm.ee
        link
        fedilink
        English
        arrow-up
        10
        arrow-down
        6
        ·
        2 months ago

        This is fair enough from an idealistic view. In practice, you don’t want your entire website to shit itself because of a potentially insignificant error.

      • bss03@infosec.pub
        link
        fedilink
        English
        arrow-up
        2
        ·
        2 months ago

        I think it’s less about type system, and more about lack of a separate compilation step.

        With a compilation step, you can have error messages that developers see, but users don’t. (Hopefully, these errors enable the developers to reduce the errors that users see, and just generally improve the UX, but that’s NOT guaranteed.)

        Without a compilation step, you have to assign some semantics to whatever random source string your interpreter gets. And, while you can certainly make that an error, that would rarely be helpful for the user. JS instead made the choice to, as much as possible, avoid error semantics in favor of silent coercions, conversions, and conflations in order to make every attempt to not “error-out” on the user.

        It would be a very painful decade indeed to now change the semantics for some JS source text.

        Purescript is a great option. Typescript is okay. You could also introduce a JS-to-JS “compilation” step that DID reject (or at least warn the developer) for source text that “should” be given an error semantic, but I don’t know an “off-the-shelf” approach for that – other than JSLint.