Decorator Metadata
Attach and read metadata with the metadata API.
Decorator Metadata is a free TypeScript Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Decorator Metadata API
Standardized decorators include a built-in metadata channel. The context object exposes a metadata object you can write to, and the class later carries it under the well-known Symbol.metadata key.
context.metadata
Every decorator context shares a single metadata object for the class being defined. Decorators attach data to it; it becomes the class metadata.
function tag(label: string) {
return function (value: any, context: DecoratorContext) {
context.metadata[context.name] = label;
};
}Reading via Symbol.metadata
After the class is defined, the collected metadata is available at SomeClass[Symbol.metadata].
class User {
@tag("identifier") id = 0;
@tag("display") name = "";
}
const meta = (User as any)[Symbol.metadata];
console.log(meta); // { id: "identifier", name: "display" }Why a Shared Object
Because all decorators on a class write to the same metadata object, you can accumulate information from many decorators into one coherent record describing the class.
Storing Structured Metadata
Metadata values can be any shape. A common pattern is keeping a nested record keyed by member name.
function column(type: string) {
return function (value: any, context: ClassFieldDecoratorContext) {
const md = context.metadata as any;
md.columns ??= {};
md.columns[context.name] = { type };
};
}Class-Level Metadata
A class decorator can also write metadata, for example a table name that complements per-field column info.
function entity(table: string) {
return function (value: Function, context: ClassDecoratorContext) {
(context.metadata as any).table = table;
};
}
@entity("users")
class UserEntity {
@column("serial") id = 0;
@column("text") name = "";
}Inheritance of Metadata
Metadata objects are linked along the prototype chain: a subclass metadata object has the parent metadata as its prototype, so inherited entries are visible unless overridden.
Symbol.metadata Polyfill
Symbol.metadata is a well-known symbol. In environments that lack it you assign a polyfill so the runtime can attach the collected object.
(Symbol as any).metadata ??= Symbol.for("Symbol.metadata");Metadata Without reflect-metadata
This API is part of the decorators proposal itself, so it does not require the separate reflect-metadata library. That library remains relevant only for the older experimental design-type emission (next lesson).
A Practical Read Helper
A small accessor centralizes reading metadata off any class, returning an empty object when none exists.
function getMeta(cls: any): Record<string, any> {
return (cls[Symbol.metadata] as Record<string, any>) ?? {};
}Why It Matters
Metadata turns decorators from one-off behavior wrappers into a declarative description of your classes. Frameworks read this description to build ORMs, validators, and routers without runtime reflection hacks.
Quick Check
Confirm your understanding of the metadata API.
Recap
The decorator metadata API gives every decorator a shared context.metadata object, surfaced on the class via Symbol.metadata. Decorators accumulate structured, inheritable metadata with no external library, enabling declarative, framework-friendly class descriptions.
Frequently asked questions
Is the “Decorator Metadata” lesson free?
Yes — the full text of “Decorator Metadata” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Decorator Metadata”?
Attach and read metadata with the metadata API. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start TypeScript Academy?
No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Decorator Metadata” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this TypeScript Academy lesson?
Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.