Currently, I am working on creating a static method factory function in TypeScript. In my previous implementation using ES6, everything was functioning well and meeting my expectations. However, upon transitioning to TypeScript, I encountered a type-casting issue that needs to be resolved. If you have any suggestions on how to fix this problem or propose a different design approach, I would greatly appreciate your assistance.
Javascript
REPL: https://repl.it/@RenjithV/NeatTepidBugs
class EventController {
static create () {
console.log('event created')
}
}
class MessageController {
static read () {
console.log('message read')
}
}
const controllers = {
'event': EventController,
'message': MessageController
}
function factory (controller, method) {
return controllers[controller][method]() // Operating as expected
}
const createEvent = factory('event', 'create') // event created
const readMessage = factory('message', 'read') // message read
TypeScript
REPL: https://repl.it/@RenjithV/EuphoricFeistyUtility
class EventController {
static create () {
console.log('event created')
}
}
class MessageController {
static read () {
console.log('message read')
}
}
const controllers = {
'event': EventController,
'message': MessageController
}
function factory (controller: string, method: string) {
return controllers[controller][method]() // Seeking solution for typecasting issue here
}
const createEvent = factory('event', 'create')
const readMessage = factory('message', 'read')