#How to create a type union based on a function return value

3 messages · Page 1 of 1 (latest)

frosty ravine
#

I have this function

function parseControllerName(name: string) {
    return lowerFirst(name.replace('Controller', ''))
}

and I have an Object which is something like

{
  AccountController: controllers.AccountController,
  AuthController: controllers.AuthController,
  ...
}

I want to make a type that would parse all of these Object keys through parseControllerName method so the type becomes this

{
  account: controllers.AccountController,
  auth: controllers.AuthController,
  ...
}
odd frost
#

you could make the function generic to use it for transforming types but imo that seems kinda excessive. you could do the transformation with just normal type transformations too

frosty ravine
#

Could you show an example, just so Im sure that I understand what you mean