How can I instantiate a class (with, say, a known empty constructor), for example:
at api/EmptyClass1.ts
, I have:
export default class EmptyClass1 {
}
and, at api/EmptyClass2.ts
, I have:
export default class EmptyClass2 {
}
I want this function:
function(filepath:string):any{
return Object.fromFile(filepath); //this line is mock code
}
to return a new instance of either EmptyClass1
or EmptyClass2
, if the parameter filepath:string
is "api/EmptyClass1.ts"
or "api/EmptyClass2.ts"
, respectively.
The files defining the classes may not be known at the time the function is written may include any number of files. Consequently, using the import
statement for each class, then using a switch, or if-then statements is not an acceptable solution.
The .ts files defining the classes are transcoded to javascript and reside in the application .build folder as .js files.
I am using typescript on node.js (recent versions).