Giter VIP home page Giter VIP logo

Comments (7)

moshe-shaham-dt avatar moshe-shaham-dt commented on July 18, 2024 3

+1
I'm also running into this issue with no fix

from reflect-metadata.

pleerock avatar pleerock commented on July 18, 2024

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.

pleerock avatar pleerock commented on July 18, 2024

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.

pleerock avatar pleerock commented on July 18, 2024

looks like the problem is in typescript decorators?

from reflect-metadata.

rbuckton avatar rbuckton commented on July 18, 2024

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.

evonox avatar evonox commented on July 18, 2024

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.

mattvb91 avatar mattvb91 commented on July 18, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.