Skip to content

A Deno-compatible copy of the TypeScript Metadata Reflection API my Microsoft

License

Notifications You must be signed in to change notification settings

cmd-johnson/deno-reflect-metadata

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Metadata Reflection API for Deno

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.

Example usage

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

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}

About

A Deno-compatible copy of the TypeScript Metadata Reflection API my Microsoft

Topics

Resources

License

Stars

Watchers

Forks