uhyo / better-typescript-lib

Better TypeScript standard library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JSON.stringify does not accept readonly Array

narumincho opened this issue Β· comments

πŸ•— Version

1.0.0

πŸ’» Code

/// <reference path="./node_modules/better-typescript-lib/lib.esnext.full.d.ts" />

const array: number[] = [0, 1, 2];
const readonlyArray: readonly number[] = [0, 1, 2];

JSON.stringify(array);
JSON.stringify(readonlyArray); // type error

πŸ™ Actual behavior

Compile error at the last line:

No overload matches this call.
  Overload 1 of 2, '(value: JSONValue, replacer?: ((this: JSONValue, key: string, value: JSONValue) => any) | undefined, space?: string | number | undefined): string', gave the following error.
    Argument of type 'readonly number[]' is not assignable to parameter of type 'JSONValue'.
      The type 'readonly number[]' is 'readonly' and cannot be assigned to the mutable type 'JSONValue[]'.
  Overload 2 of 2, '(value: JSONValue, replacer?: (string | number)[] | null | undefined, space?: string | number | undefined): string', gave the following error.
    Argument of type 'readonly number[]' is not assignable to parameter of type 'JSONValue'.
      The type 'readonly number[]' is 'readonly' and cannot be assigned to the mutable type 'JSONValue[]'.ts(2769)

πŸ™‚ Expected behavior

no compile error

πŸ›  Workaround

JSON.stringify(readonlyArray as JSONValue);

Basically it feels a bit inconvenient for those who want to use ReadonlyArray for arrays.

γ¨γ™γ‚Œγ°γ‚¨γƒ©γƒΌγ―ζΆˆγˆγ‚‹γ‘γ©, εŸΊζœ¬ηš„γ« ι…εˆ—γ« ReadonlyArray γ‚’ δ½Ώγ„γŸγ„θΊ«γ¨γ—γ¦γ―ε°‘γ—δΈδΎΏγ«ζ„Ÿγ˜γ‚‹ζ°—γŒγ—γΎγ™.

commented

Thank you for reporting this! πŸ™‚
Fixed in v1.0.1.