Comments (7)
+1
I'm also running into this issue with no fix
from reflect-metadata.
looks like same issue when you are passing class to annotation:
import 'reflect-metadata';
function Relation(cls) {
return function () {
console.log(cls);
}
}
class Person {
name: string;
@Relation(PersonInfo)
info: string;
}
class PersonInfo {
info: Person;
}
prints "undefined"
But if we do:
import 'reflect-metadata';
function Relation(cls) {
return function () {
console.log(cls);
}
}
class PersonInfo {
info: Person;
}
class Person {
name: string;
@Relation(PersonInfo)
info: string;
}
from reflect-metadata.
If I use seprate files and put Relation annotation in each class I have the same problem - one of the is undefined.
import {Person} from "./Person";
import {Relation} from "./Relation";
export class PersonInfo {
@Relation(Person)
info: string;
}
import {Relation} from "./Relation";
import {PersonInfo} from "./PersonInfo";
export class Person {
name: string;
@Relation(PersonInfo)
info: string;
}
export function Relation(cls) {
return function () {
console.log(cls);
}
}
import {Person} from "./Person";
let person = new Person();
result:
undefined
[Function: PersonInfo]
from reflect-metadata.
looks like the problem is in typescript decorators?
from reflect-metadata.
This is more of an issue with how we emit design-time metadata decorators in TypeScript. I'll close this bug so we can continue the relevant discussion on microsoft/TypeScript#4521.
from reflect-metadata.
I have a proposal for workaround to address this issue. I include a Node Typescript file as an attachment to this post. Basically the script uses two regular expressions, the first one to extract all functions called __decorate and then the second one to find constructor function overrides mainly used IMHO by class decorators. All of these piece of code are then placed to the end of file, replacing a comment that must be placed at the end of TS file. Currently I tested it only on the Alf Abstract Syntax expressed using the MOF serialized to JSON (using decorators of class-validator library).
import fs = require("fs");
// Enter here the file with domain model you want to update
const file = "./dist/models/mda-mof-model/mda-mof-model.js";
// Read the file content
let content = fs.readFileSync(file).toString();
// Extract all pieces of code containing the "__decorate" function
let matches = content.match(/([A-Za-z0-9_ =\.])+__decorate.*?\);/sg);
// Replace all found pieces of code with empty string
// Index from one to ignore the definition of "__decorate" function at the top of the file
for(let i = 1; i < matches.length; i++) {
content = content.replace(matches[i], "");
}
// Join the found pieces of code into one variable named "decorators"
let decorators = "";
for(let i = 1; i < matches.length; i++) {
decorators += matches[i] + "\n";
}
// Extract all constructor function overrides, IMHO used by class decorators mainly
matches = content.match(/^(?![ ]*this.*$)[ ]*([A-Za-z0-9_ \.]+[ ]*=[ ]*[A-Za-z0-9_ \.]+;)$/gm);
// Replace them with empty string and add them behind the decorator functions definitions
for(let match of matches) {
content = content.replace(match, "");
decorators += match + "\n";
}
// At the bottom of the TS file replace the comment with decorator definitions
content = content.replace("/* DECORATORS */", decorators);
// Save the updated content to the file
fs.writeFileSync(file, content);
from reflect-metadata.
@rbuckton has there been an official fix for this? The linked issue seems to be closed without being resolved? Not sure where to get the types from at the moment.
from reflect-metadata.
Related Issues (20)
- getMetadata("design:paramtypes") returns [ undefined ] HOT 2
- Inferred types are set as Object HOT 4
- TypeError: Cannot read property 'Reflect' of undefined HOT 1
- Tag new version in upstream HOT 4
- How to get the parameter type of ParameterDecorator
- "design:type" return undefined if types are defined out of order. HOT 2
- Importable version of `reflect-metadata` that does not shim globals. HOT 6
- index.d.ts file, what is the type of function metadata HOT 1
- The use of "indexOf" is not rigorous enough
- Since language specification published, this repo should be archived HOT 4
- Reflect metadata circular dependency HOT 2
- CJS / ES Module? HOT 12
- Throw type error when TS class decorator returns an object. HOT 7
- How to import (using absolute paths import feature of typescript) an module which use reflect-metadata?
- process.env check outputs complete dump of environment settings HOT 4
- Remove dynamic evaluation to access globalThis in "lite" mode HOT 1
- Infinite loop in v0.2.0 HOT 2
- Package subpath './Reflect' is not defined by "exports" HOT 3
- Stage 3 decorators additional implementation HOT 1
- Reflect-Metadata update breaks Nest+TypeORM HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from reflect-metadata.