Как сузить тип от typeof T до T

#typescript #typescript-typings #typescript2.0 #typescript-generics

#typescript #typescript-типизации #typescript2.0 #typescript-общие сведения

Вопрос:

У меня есть функция со следующей подписью.

  waitMessage<T extends IIPCMessagesConstructors>(wantedMessageType: T): Promise<// ?? //>
  

Где IIPCMESSAGESCONSTRUCTURES определяются следующим образом.

 export type IIPCMessagesConstructors = typeof OSUserRegistrationMessage |
typeof AppLaunchRequestMessage |
typeof OSUserRegistrationResponseMessage |
typeof UserApplicationRegistrationMessage |
typeof UserApplicationRegistrationResponseMessage |
typeof AppLaunchResponseMessage |
typeof InstallationCompleteMessage |
typeof InstallationInitCompleteMessage |
typeof InstallationProgressMessage |
typeof InstallationStartMessage |
typeof InstallationStopMessage;
  

Моя функция WaitMessage возвращает экземпляр предоставленного конструктора. Итак, как я могу преобразовать typeof T в T?

Комментарии:

1. Используйте InstanceType<Constructor>

Ответ №1:

InstanceType<IIPCMessagesConstructors> сделано