question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Decorator Metdata: expose array element type

See original GitHub issue

TSC 175, target ES6, Node 5.6

Sample class with decorated properties:

@sg.DMClass
export class User implements IUser {
    @sg.DMProperty
    firstName: string;
    @sg.DMProperty
    lastName: string;
    @sg.DMProperty
    friends: User[];
}

Property decorator:

export function DMProperty(target: any, key: string) {
    var type = Reflect.getMetadata("design:type", target, key);
}

Current behaviour: When the decorator gets called for property “friends”, “type” is “Array” and the captured type (“User”) is lost.

Expected behaviour “type” should contain the captured type (“User”), too

Following very dirty work-around does the job: I modified tsc’s method “emitSerializedTypeNode”:

function emitSerializedTypeNode(node) {
    if (node) {
        console.log("*** emitSerializedTypeNode: node.kind: ", node.kind);
        switch (node.kind) {
            ...
            case 157:
                // Begin dirty ---------------------------------------------------------------------------
                var _elemType = node.elementType.typeName.text;
                write("{name: 'Array<" + _elemType + ">', type:'Array', elemType:'" + _elemType + "'}");
                console.log("*** emitSerializedTypeNode: Array, type: ", node.elementType.typeName.text);
                // End dirty -----------------------------------------------------------------------------
                return;
            case 150:
            ...

Best Mind

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:54
  • Comments:43 (4 by maintainers)

github_iconTop GitHub Comments

21reactions
IonelLupucommented, Aug 27, 2018

You may see low reaction numbers for this feature but it is waited by a lot of people. We need to transform JavaScript/Typescript and make it compete with languages like Java.

For example, this feature will allow me to build a very beautiful database ORM just by using class properties and no any workarounds with decorators.

19reactions
listepo-alterpostcommented, Sep 19, 2017

Hi, any news?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Decorators - TypeScript
A Decorator is a special kind of declaration that can be attached to a class ... When getFormat is called, it reads the...
Read more >
get List of reflect-metadata decorated fields of class
I composed my own property decorator and it's called Field . How to get list of fields/properties, which are decorated by Field ,...
Read more >
@Decorator · ng-metadata
A decorator for adding NgModule metadata to a class. NgModules are how we register our dependencies (such as Component, Directives, Pipes and Providers), ......
Read more >
How to preserve Function Metadata while using Decorators?
Copying decorator metadata is an important part of writing decorators. If you forget to use @wraps, you'll find that the decorated function ...
Read more >
Types and Fields - TypeGraphQL
It marks the class as the type known from the GraphQL SDL or ... To do this, we use the @Field decorator, which...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found