2

I have class like

 export class Model {
   name: string;
   size: number;
}

if I try to get typeof these variables like below, it returns undefined.

model: Model;
 .
 .
 .
this.model = new Model();
console.log(typeof (this.model.size));

I tried to make class with constructor.

export class Model{        
        constructor(public q: string, public size?: number) {}
}

And I tried to get type of variables.

 this.model = new Model('', null);
 console.log(this.model.size)

It returns object. I tried toType method

toType = function(obj) {
    return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
  }

And

console.log( this.toType (this.model.size));

It returns null.

How can I get variable type correctly without assigning values?

2
  • Is this in typescript? Because there's a lot of things in your question that aren't valid JS Commented Apr 16, 2018 at 6:16
  • Yes. typescript Commented Apr 16, 2018 at 6:39

2 Answers 2

2

You can use the emitDecoratorMetadata and experimentalDecorators option in conjunction with the reflect-metadata package

import 'reflect-metadata'
// Decorator does nothing but metadata is emitted only for decorated symbols by the compiler so we need an empty decorator
function EmitType(target: Object, propertyKey: string | symbol){

}

export class Model {
    @EmitType name: string;
    @EmitType size: number;
    @EmitType child: Model;
    @EmitType data :  { test: string };
}

// Will output function String() { … }
console.log(Reflect.getMetadata("design:type", Model.prototype, "name"));

// Will output function Number() { … }
console.log(Reflect.getMetadata("design:type", Model.prototype, "size"));

// Will output function Model() { … }
console.log(Reflect.getMetadata("design:type", Model.prototype, "child"));

// Will output function Object() { … }
console.log(Reflect.getMetadata("design:type", Model.prototype, "data"));

The value stored as metadata is the type constructor, so for custom classes it will be the constructor for the class. For interfaces and other types that do not have constructors, the type will always be Object

Sign up to request clarification or add additional context in comments.

Comments

0

A lot of the code you have shared does not appear to be valid JS. Another pattern I noticed was that you were never actually assigning the values to instance properties in your classes.

Here is a simple example. Hope it helps.

class Test { 
  constructor(a,b){
    // Make sure you assign the values to instance properties
    this.a = a;
    this.b = b;
  }
} 

// Create a new instance passing different typed arguments
var t = new Test('one',1);

// Test the types
console.log(typeof t.a); //string
console.log(typeof t.b); //number

1 Comment

Yes I know. I want to get type of variables dynamically without assigning values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.