9

Hiii devRanters, I have a TypeScript question for you...
How do I dynamically import classes?

I have a class like this:
export default class Foo implements Bar {
...
....public getName(): string {
........return this.name;
....}
}

and then I have another file with this:
import(`./class-cmd/${file}`).then((command) => {
....winston.debug(command);
....winston.debug(command.getName());
});

the first command spits out something like this:
debug: default=class Foo {
....constructor() {
........this.name = "foo";
....}
....getName() {
........return this.name;
....}
}

and I would expect that the second command will work, however it throws this:
UnhandledPromiseRejectionWarning: TypeError: command.getName is not a function

Any idea what I might be doing wrong?

Comments
  • 3
    oooh... :( my formatting... damn you dfox

    edit: fixed it with dots...
  • 5
    If you post this on stack overflow with that formatting, the down voters will personally come to your home and kill you.
  • 2
    @mohammed meh... what else would you use on devRant? :D
    And I won't go to SO even if you gave me 100€ if I did... I like my current mental state
  • 0
    @D3add3d I'm just kidding 😁
    I think a screen shot would be better, or a gist on github.
  • 1
    @mohammed maaybe... but the code is just to illustrate what I am talking about, the actual question is on the second line of the post :)
  • 0
    I came across something similar recently, but with json instead of ts. I wanted to import a json file in an angular project and it didn't work out of the box.
    The solution was to edit the typings.d.ts and add some lines. I think the solutions also showed how to define the type of other imports. If you haven't searched in this direction or tried searching for json-imports in typescript before you could try this.
  • 1
    @YouAreAPIRate JSON imports actually work flawlessly for me :)
  • 1
    @D3add3d what i meant was maybe those two things are linked. But i don't think they are.

    I did some experimenting and found a solution. At first i thought some typescript configuration was the solution (appearently you can provide custom type information via index.d.ts files). But the solution that worked for me (no warnings in vs code, not tested in production) was this:

    import(...).then((mycls: any) =>{
    var instance = <clstype> new mycls(...);
    ...
    })

    You just provide type information yourself. I don't know if there is a better type for mycls than any, but typescript has no class-generic. Still, typescript lets you treat it as constructor and with that you only have to cast the result to the type you know it will be.
  • 1
  • 0
    πŸ“Œ
  • 0
    For something like this a question section only for programming would be nice to have. Like... a small SO just with devRant people
  • 0
  • 0
    @YouAreAPIRate going to test that now but I have strong feeling it won't work
  • 0
    @YouAreAPIRate UnhandledPromiseRejectionWarning: TypeError: command is not a constructor

    Sooo yea... I don't get any errors in IntelliJ IDEA, it's a runtime error
  • 0
    I use require when I have local json
  • 3
    HOLY SHIT!!! I GOT IT TO WORK!!

    HUGE THANKS to this random person on SO who did not get a single upvote :-/
  • 0
    @lines ...not JSON, I am importing classes
  • 1
    @jeeper *beep* ^^^
Add Comment