Saturday, April 13, 2024
 Popular · Latest · Hot · Upcoming
116
rated 0 times [  120] [ 4]  / answers: 1 / hits: 23791  / 7 Years ago, wed, december 21, 2016, 12:00:00

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?


More From » node.js

 Answers
7

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



npm install reflect-metadata -D


inside your .ts file:



 import reflect-metadata;

[#59619] Monday, December 19, 2016, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
karladaijahf

Total Points: 78
Total Questions: 123
Total Answers: 89

Location: Liechtenstein
Member since Wed, Dec 8, 2021
2 Years ago
;