'Accessing object property when object consists of classes
With an object consisting of references to classes:
// commands/index.ts
export default {
Authorize,
BootNotification,
...
UpdateFirmware,
};
How do I go about creating variables and instantiate dynamically from this object? For something like below I get the ts error:
TS2538: Type '{ Authorize: typeof Authorize; BootNotification: typeof BootNotification; CancelReservation: typeof CancelReservation; ChangeAvailability: typeof ChangeAvailability; ... 23 more ...; UpdateFirmware: typeof UpdateFirmware; }' cannot be used as an index type.
Code:
import commands from "./commands";
let commandNameOrPayload: typeof commands;
let commandPayload: any;
let CommandModel: typeof commands;
CommandModel = commands[commandNameOrPayload];
commandRequest = new CommandModel(commandPayload);
For the last line of newCommandModel I'm getting the error: TS2351: This expression is not constructable.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
