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 γ δ½ΏγγγθΊ«γ¨γγ¦γ―ε°γδΈδΎΏγ«ζγγζ°γγγΎγ.
Thank you for reporting this! π
Fixed in v1.0.1.