This is a direct copy of the Metadata Reflection API by Microsoft with slight changes to make it usable in Deno.
Check out the Source Repository for more details.
import { Reflect } from "https://deno.land/x/reflect_metadata@v0.1.12-2/mod.ts";
// deno-lint-ignore no-explicit-any
type Constructor<T = unknown> = new (...args: any[]) => T;
function decorator<T>(_: Constructor<T>): void {}
@decorator
class Example {
constructor(a: string, b: number, c: Example) {}
}
console.log(Reflect.getMetadata("design:paramtypes", Example));
// "[ [Function: String], [Function: Number], [Function: Example] ]"
The decorator is required for the TypeScript compiler to generate metadata for the Example class.
If you don't put a decorator on the Example class, the call to getMetadata
will return undefined
.
Remember to always add a
tsconfig.json
file with the following content and running your code usingdeno run -c tsconfig.json your_code.ts
or decorators and reflection will not work!
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}