Files
scriptc/tests/diagnostics/json-class-fields.ts
T
Chris Tate 8d94e5d993 Refuse RegExp and Promise class fields in JSON.stringify
A Date field reads through the class's checked-dynamic property view and
writes its toJSON text, so json-class-fields.ts compiled and matched Node
while the diagnostics test expected a refusal. The Date cases move to
6000-json-stringify-class-instances.ts.

RegExp and Promise fields read through the view but the runtime JSON
traversal has no writer for them: a RegExp field threw at run time where
Node writes {}, and a Promise field failed to link. Both are now refused
at compile time naming the field, also inside a field's arrays, records
and unions, and the fixture covers them.
2026-10-18 18:34:09 -05:00

23 lines
587 B
TypeScript

// JSON.stringify reads class instances through their checked-dynamic
// property view. A class whose declared field holds a RegExp or a Promise,
// which the runtime JSON traversal cannot write, is refused at compile
// time, naming the field.
class Pattern {
name = "digits";
source = /\d+/g;
}
class Job {
title = "fetch";
pending = Promise.resolve(1);
}
class Catalog {
title = "team";
patterns: Pattern[] = [new Pattern()];
}
console.log(JSON.stringify(new Pattern()));
console.log(JSON.stringify(new Job()));
console.log(JSON.stringify({ catalog: new Catalog() }));