1. 8
  1.  

  2. 4

    I laughed when I saw this:

    // eslint-disable-next-line fp/no-loops
    for (…) {
    
    1. 1

      I wasn’t sure where you were seeing code like that, as it’s not in the README. I found that these three implementation files in src/ disable fp/no-loops.

    2. 1

      What’s the performance cost though? Seems like something that should be easily fixable with typescript and ensuring the data you stringify matches a valid JSON type.

      1. 1

        You’re correct that TypeScript could catch some of the same problems, specifically invalid JSON types (bigints, functions, symbols, classes, etc.). Except for a few ones (NaN and Infinity cannot be literal types in TypeScript, i.e. could not be detected with TypeScript only).

        However, it would not be able to detect (nor fix) most reasons why a value might throw with JSON.serialize(): circular values, infinite toJSON(), very large fields, exceptions inside toJSON(), getters or proxies, etc.

        Also, it would only detect those issues at build-time, which would not work with dynamic values.

        That being said, if the values are not dynamic and you’re only concerned with invalid JSON types (not exceptions thrown by JSON.serialize()) then I agree that TypeScript would be a better fit there. Thanks for pointing that out!

        1. 2

          Thanks for the explanation. I have not done much web dev in a long time, this extra context makes a ton of sense.

          1. 1

            Bigints are not invalid JSON; JSON declares no limitations on the length of numeric values.

            1. 1

              In JSON itself, you’re right. Integers in a JSON string do not have a length limit.

              However, I was referring to BigInt as a JavaScript type. BigInts make JSON.serialize() throw.

              It is possible to circumvent this by serializing them to strings first via bigint.toString(), then parsing them back from a string too. Or do the same with floats. However, as its own type (without conversion to another type), they are not valid JSON otherwise.