0

I would like to know how to instantiate a class knowing that the type of the class by a STRING under Nestjs and Typescript. I have done some research using this but I have an error that WINDOWS is not defined.

let paul:string = "Wall";

let test = Object.create(new window[paul].prototype);

console.log(test.constructor.name); //Wall

ReferenceError: window is not defined

2
  • window is the global object in browser side JavaScript. It's not clear what you're trying to do at the moment as in, why would you try to use a string to create a class? Commented Jul 23, 2020 at 14:27
  • I want several objects that have different types. These types I know by reading them in a text file as a string For example, in my file I have this: WALL, 'Adrien' HOUSE, 'Pierre' I would like to do this automatically: let adrien = new WALL(); let pierre = new HOUSE(); Except that WALL and HOUSE can change depending on the file. The result should be something like this let string1 = "WALL" let paul = new string1(); Commented Jul 23, 2020 at 14:33

2 Answers 2

1

What you're trying to do is not only extremely complicated in better-suited languages but I doubt it's even possible in Typescript...

A better solution is to plan the whole thing differently (better) and go with inheritance. your Paul class probably inherits from the Person interface just the same as your Sue class so you instantiate with the parent interface

let me rephrase that... you could instantiate with a

let test: Person

switch(name) {
    case 'Paul':
        test = new Paul()
}

which would serve your purpose.. My guess is you're trying to do that cause you wanna later do something like

let people: any[] = //...
for(let person of people) {

}

but you should instead do

let people: Person[] = //...
for(let person of people) {

}

and any class that implements the Person interface would be able to fit your people array

But again, if you really need a shit ton of classes all with different names, then it begs the question, do you really need separate classes?

UPDATE: I saw your comment about

WALL, 'Adrien' HOUSE, 'Pierre'

In that case you'd use a switch

let houses: House[] = []
let walls: Wall[] = []

... 

switch(key) {
    case 'WALL':
        walls.push(new Wall(value))
    case 'HOUSE':
        houses.push(new House(value))

    ...

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

8 Comments

Yes I can use switch but I have 30 class. So, it is not very beautiful a switch with 30 case
Hence my reasoning for planning the whole thing differently (better-ly) ;)
A better solution would be to have a json column in your database and just store them that way or even better use a json based database such as mongodb ... Ofc I would have to know all of your project to give better advice and I don't.. but I'm willing to bet your solution is unecessarily overcomplicated.. like think about do you reeeally need all those classes or are they just there cause your teacher told you it was cleaner? Cause spoiler alert it won't be cleaner
And if you reeeally want to automate the database thing it means two things to me: A) you're trying to reinvent the wheel instead of using a properly maintained library B) you're biting way more than you can chew.. again I'm sorry if I sound passive aggressive but I'm really trying to help you..
Also the eval() thing literally cannot work because of what typescript is and how it works.. again you're biting more.than you can chew.. trust me, what you're doing is very low level, typescript is not an appropriate language to do that for (if you even can) and you'd be much better off trying to find different solutions
|
0

Without having too much information on what you are actually trying to build it sounds like you want to create generic objects that contain information based on your text file.

If you want to instantiate a class based on what you parsed in your text file, you can use a simple if statement:

let object: any = undefined;
if (parsedString === "Wall) {
object = new Wall();
}

If you actually don't have a wall class and you want to create a "Wall" object, I wonder what the use would be, since it would not have any wall behavior and therefore it is not better than a simple string variable. Since instead of object.constructor.name you could just assign a string with the name to the variable instead of instantiating the empty object.

let objectName: string = "";
objectName = parsedString;

Or if it has to be an object just assign a name property to an empty object:

let myObject: object = {};
myObject.name = parsedString;

I do think you have a design flaw in what you are trying to do and if you can give us a better description I'm sure we can help you out in how to design your software properly to achieve your goal.

Here is more to read on how to dynamically assign properties to objects in Typescript

1 Comment

this would work but I'd argue it would also defeat the purpose of having classes at all... (as you do too)

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.