mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-11-07 08:33:50 +08:00
22 lines
562 B
TypeScript
22 lines
562 B
TypeScript
// Object literals with get/set accessors keep their closures in reserved
|
|
// slots, so their typed values have no JSON form: Node's JSON.stringify
|
|
// invokes the getter, which the typed serializer cannot do.
|
|
|
|
const counter = {
|
|
count: 1,
|
|
get doubled(): number {
|
|
return 2;
|
|
},
|
|
};
|
|
|
|
// JSON.stringify of the typed value would omit the getter's property.
|
|
console.log(JSON.stringify(counter));
|
|
|
|
// A literal written into an `any` slot directly keeps its accessor.
|
|
const direct: any = {
|
|
get value(): number {
|
|
return 3;
|
|
},
|
|
};
|
|
console.log(direct.value);
|