File tree Expand file tree Collapse file tree 2 files changed +28
-4
lines changed Expand file tree Collapse file tree 2 files changed +28
-4
lines changed Original file line number Diff line number Diff line change @@ -209,6 +209,16 @@ type JSONValue =
209
209
}
210
210
| JSONValue [ ] ;
211
211
212
+ type ReadonlyJSONValue =
213
+ | null
214
+ | string
215
+ | number
216
+ | boolean
217
+ | {
218
+ readonly [ K in string ] ?: ReadonlyJSONValue ;
219
+ }
220
+ | readonly ReadonlyJSONValue [ ] ;
221
+
212
222
interface JSON {
213
223
/**
214
224
* Converts a JavaScript Object Notation (JSON) string into an object.
@@ -227,8 +237,12 @@ interface JSON {
227
237
* @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
228
238
*/
229
239
stringify (
230
- value : JSONValue ,
231
- replacer ?: ( this : JSONValue , key : string , value : JSONValue ) => any ,
240
+ value : ReadonlyJSONValue ,
241
+ replacer ?: (
242
+ this : ReadonlyJSONValue ,
243
+ key : string ,
244
+ value : ReadonlyJSONValue
245
+ ) => any ,
232
246
space ?: string | number
233
247
) : string ;
234
248
/**
@@ -238,7 +252,7 @@ interface JSON {
238
252
* @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
239
253
*/
240
254
stringify (
241
- value : JSONValue ,
255
+ value : ReadonlyJSONValue ,
242
256
replacer ?: ( number | string ) [ ] | null ,
243
257
space ?: string | number
244
258
) : string ;
Original file line number Diff line number Diff line change @@ -78,7 +78,17 @@ expectType<{ foo: number; bar: string; baz: boolean }>(
78
78
} ) ( ) ;
79
79
80
80
// JSON
81
- expectType < JSONValue > ( JSON . parse ( "{}" ) ) ;
81
+ {
82
+ expectType < JSONValue > ( JSON . parse ( "{}" ) ) ;
83
+ const arr = [ 1 , 2 , "foo" ] ;
84
+ expectType < string > ( JSON . stringify ( arr ) ) ;
85
+ const obj = { foo : { bar : 1 } } ;
86
+ expectType < string > ( JSON . stringify ( obj ) ) ;
87
+ const readonlyArr = [ 1 , 2 , 3 ] as const ;
88
+ expectType < string > ( JSON . stringify ( readonlyArr ) ) ;
89
+ const readonlyObj = { foo : { bar : 1 } } as const ;
90
+ expectType < string > ( JSON . stringify ( readonlyObj ) ) ;
91
+ }
82
92
83
93
// ArrayConstructor
84
94
{
You can’t perform that action at this time.
0 commit comments