Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
82
rated 0 times [  85] [ 3]  / answers: 1 / hits: 18037  / 8 Years ago, tue, october 4, 2016, 12:00:00

In my application, I have something like this:



user.ts



export class User { ... }


and right now, I do this:



app.component.ts



callAnotherFunction(User);


How can I do this if I have the class name as a string, that is User? If possible, how would I check if the variable really is a usable class?



For example:



let test = User;

if (**test is really a usable class**) {
console.log(Yay!);
callAnotherFunction(**something to put here**);
} else {
console.log(Error!);
}


I am not sure if this is even possible, but thanks for your input already.






All I have found so far is examples like these:



let test = User;
let user = new window[test]();


But user would then be an instance of User. Additionally, it does not seem to work in my Angular 2 application - probably a scope error. If I wanted to try this, what would I need to use instead of window?


More From » class

 Answers
6

This sounds like a reflection question in typescript.



Possibly you could check this out if you haven't



Dynamically loading a typescript class (reflection for typescript)


[#60505] Sunday, October 2, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nora

Total Points: 248
Total Questions: 111
Total Answers: 97

Location: India
Member since Wed, Aug 4, 2021
3 Years ago
;