Skip to main content

@dx/reflect@0.2.6

forked from jiawei397/deno-reflect-metadata

Works with
This package works with Deno
JSR Score
70%
Published
2 weeks ago

Metadata Reflection API for Deno

JSR Version JSR Score ci

This is a fork of jiawei397/deno-reflect-metadata

This is a 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.

Example usage

type ClassConstructor<T = unknown> = new (...args: any[]) => T;

function Decorator<T>() {
  return (_: ClassConstructor<T>): void => {};
}

class ClassA {}

@Decorator()
class ClassB {
  constructor(a: string, b: number, c: ClassA) {}
}

const metadata = Reflect.getMetadata('design:paramtypes', ClassB);

console.log(metadata?.map((x: ClassConstructor) => x.name).join(', '));
// "String, Number, ClassA"

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 using deno run -c tsconfig.json your_code.ts or decorators and reflection will not work!

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}
Built and signed on
GitHub Actions
View transparency log

Add Package

deno add @dx/reflect

Import symbol

import * as mod from "@dx/reflect";