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

标签 typescript

如何为 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: ?) {

  }


}
所以我。只有 DoctorSignupOutput 类型的对象当模板等于 DOCTOR_SIGNUP 时允许?

最佳答案

试试看

  createMessage<
    T extends keyof typeof EMAIL_TEMPLATES, // <- T points to a key
    R extends (typeof EMAIL_TEMPLATES)[T] // <- R points to the type of that key
  >(template: T, params: R) {

  }

一个详细的例子:

interface MyInterface { 
    keyString: string;
    keyNumber: number;
}

function createMessage<
  O extends object, // object itself
  K extends keyof O, // all its keys
  V extends O[K] // types of keys, once K is specified, it's narrowed to related type.
>(object: O, key: K, value: V) {
    console.log(object, key, value);
}

const data: MyInterface = {
    keyString: 'hello',
    keyNumber: 777,
};

createMessage(data, 'keyString', 'world'); // works
createMessage(data, 'keyNumber', 42); // works

createMessage(data, 'keyString', 42); // fails due to wrong type
createMessage(data, 'keyNumber', 'world'); // fails due to wrong type
createMessage(data, 'keyUnknown', 'random'); // fails due to missed key in the original interface.

Playground

关于基于第一个参数的 typescript 第二个参数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61734515/

相关文章:

typescript - 具有多个类型参数的泛型中的推断类型

javascript - 与 useEffect 一起使用时,自定义钩子(Hook)会被无限调用

javascript - 具有多个条件的 if 语句不起作用

JavaScript 语法错误 : Unexpected end of input

json - 来自导入文件而非数据库的 Google map 坐标

typescript - 导出时声明为联合类型的常量降级为字符串

typescript - 按值从 typescript 联合中选择元素

javascript - 如何为 material-ui 组件正确使用 typescript 模块扩充?

sockets - 删除带有套接字的消息

Angular 6 : View is not updated when Subscribe method is called