Skip to main content
This package has been archived, and as such it is read-only.

@dx/reflect@0.2.15
Built and signed on GitHub Actions

forked from jiawei397/deno-reflect-metadata

This package works with Deno
This package works with Deno
JSR Score
100%
Published
2 months ago (0.2.15)

[Deprecated] Metadata Reflection API for Deno

Please use the original Package deno add npm:reflect-metadata (https://www.npmjs.com/package/reflect-metadata).

JSR Version JSR Score ci

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

This includes 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 jsr:@dx/reflect

Import symbol

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

---- OR ----

Import directly with a jsr specifier

import * as reflect from "jsr:@dx/reflect";