Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
If you post this on stack overflow with that formatting, the down voters will personally come to your home and kill you.
-
@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 -
@D3add3d I'm just kidding π
I think a screen shot would be better, or a gist on github. -
@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 :)
-
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. -
@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. -
For something like this a question section only for programming would be nice to have. Like... a small SO just with devRant people
-
@YouAreAPIRate UnhandledPromiseRejectionWarning: TypeError: command is not a constructor
Sooo yea... I don't get any errors in IntelliJ IDEA, it's a runtime error
Related Rants
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?
question
classes
dynamic
import
typescript