declare Fields Skip Runtime Emission
Use declare class fields so they emit no runtime property, assign them later with Object.assign, and verify ownership before and after hydration.
Code
class HydratedRecord {
declare id: number;
declare payload: Record<string, unknown>;
}
const record = new HydratedRecord();
console.log("own id before:", Object.hasOwn(record, "id"));
console.log("own payload before:", Object.hasOwn(record, "payload"));
Object.assign(record, {
id: 7,
payload: { ok: true, source: "api" },
});
console.log("own id after:", Object.hasOwn(record, "id"));
console.log("id:", record.id);
console.log("payload ok:", (record.payload as { ok: boolean }).ok);
console.log("keys:", Object.keys(record).join(","));Output
Captured from a sandboxed run of this snippet in the build pipeline.
own id before: false
own payload before: false
own id after: true
id: 7
payload ok: true
keys: id,payloadWhat the code establishes
The snippet is organized around the class HydratedRecord.
The demo data lives in module-level bindings: record.
Watch out for
- No dependencies section needed; the standard library is the whole story.
Applying it
The demo output is labeled (own id before:, own payload before:, own id after:), so every printed line traces back to the operation behind it.
Statements that matter
declare payload: Record<string, unknown>;
const record = new HydratedRecord();
declare id: number;
Interactive Tools for This Topic
FAQ
- Do I need to install anything before running this?
- No — every API used here ships with the language runtime, so there is nothing to install. Its state lives in module-level bindings: `record`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.