Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import/use Reflect in typescript

I'm trying to use the code in this SO answer. It uses Reflect. Here's a copy:

export function CustomComponent(annotation: any) {
  return function (target: Function) {
    var parentTarget = Object.getPrototypeOf(target.prototype).constructor;
    var parentAnnotations = Reflect.getMetadata('annotations', parentTarget);

    var parentAnnotation = parentAnnotations[0];
    Object.keys(parentAnnotation).forEach(key => {
      if (isPresent(parentAnnotation[key])) {
        annotation[key] = parentAnnotation[key];
      }
    });
    var metadata = new ComponentMetadata(annotation);

    Reflect.defineMetadata('annotations', [ metadata ], target);
  }
}

First, I got these two errors:

Property 'getMetadata' does not exist on type 'typeof Reflect'.
Property 'defineMetadata' does not exist on type 'typeof Reflect'.

Then I ran npm install reflect-metadata, but I don't know how to use it.

import { Reflect } from reflect-metadata;

Module '".../node_modules/reflect-metadata/index"' has no exported
member 'Reflect'.

Or

import { Reflect } from 'reflect-metadata/Reflect';

Cannot find name 'Record'. 
Type '{}' is not assignable to type 'V'.
File '.../node_modules/reflect-metadata/Reflect.ts' is not a module.

Or

import "reflect-metadata"

rollup: Treating 'fs' as external dependency 
bundle update failed: Error transforming .../node_modules/typescript/lib/typescript.js
with 'commonjs' plugin: The keyword 'package' is reserved (57066:28) in
.../node_modules/typescript/lib/typescript.js 

Or

var reflect = require("reflect-metadata");

Cannot find name 'require'.

Or

declare var require: any;
var reflect = require("reflect-metadata");
var Reflect = reflect.Reflect;

    rollup: Treating 'fs' as external dependency 
bundle update failed: Error transforming .../node_modules/typescript/lib/typescript.js
with 'commonjs' plugin: The keyword 'package' is reserved (57066:28) in
.../node_modules/typescript/lib/typescript.js

Surely I'm just missing something silly, even a typo. What can I do to use this code?

like image 885
jmilloy Avatar asked Dec 21 '16 19:12

jmilloy


People also ask

Does TypeScript have reflection?

Summary. The reality is that Typescript Reflection is very poor. In order to get a very basic set of Reflection features — we need to make a number of hacky solutions. Other than Dependency Injection tools, I see no other use for this limited functionality.

Can I import a TypeScript function in JavaScript?

Not at all, because Typescript cannot be directly executed by Javascript. You must have some tooling that takes care of converting Typescript to executable Javascript, the answer lies there.

What is the use of Reflect-metadata?

reflect-metadata Allows you to do runtime reflection on types. The native (non reflect-metadata) version of type inference is much poorer than reflect-metadata and consists only of typeof and instanceof .


2 Answers

you have to import the type declarations together with the (js) library

npm install reflect-metadata -D

inside your .ts file:

 import "reflect-metadata";
like image 103
hannes neukermans Avatar answered Sep 25 '22 04:09

hannes neukermans


As of today, @types/reflect-metadata is deprecated since reflect-metadata includes already its own typings.

So, to use them you just need to import the library (Uses the global scope). That's it.

  1. Install it:
    npm install reflect-metadata --save

  2. Import it:
    import 'reflect-metadata';

like image 34
Gabriel G. Avatar answered Sep 22 '22 04:09

Gabriel G.