Typescript基于第一个参数的第二个参数类型

Typescript基于第一个参数的第二个参数类型,typescript,Typescript,如何强制tcreateMessage方法使用正确的第二个参数类型 const EMAIL_TEMPLATES = { DOCTOR_SIGNUP: DoctorSignupOutput, CHANGE_PASSWORD: PasswordParams } as const; @Injectable() export class EmailService { sendEmail() { console.log('Email sent!'); } createMes

如何强制t
createMessage
方法使用正确的第二个参数类型

const EMAIL_TEMPLATES = {
  DOCTOR_SIGNUP: DoctorSignupOutput,
  CHANGE_PASSWORD: PasswordParams
} as const;

@Injectable()
export class EmailService {

  sendEmail() {
    console.log('Email sent!');
  }

  createMessage(template: keyof typeof EMAIL_TEMPLATES, params: ?) {

  }


}
因此,当模板等于
DOCTOR\u-SIGNUP

时,仅允许类型为
DoctorSignupOutput
的对象

createMessage<

T扩展了电子邮件模板的类型,//这是我们都喜欢的:)