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?
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.
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.
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 .
you have to import the type declarations together with the (js) library
npm install reflect-metadata -D
inside your .ts file:
import "reflect-metadata";
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.
Install it:npm install reflect-metadata --save
Import it:import 'reflect-metadata';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With