打字稿基于第一参数的第二参数类型

时间:2020-05-11 16:27:01

标签: 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的对象吗?

1 个答案:

答案 0 :(得分:6)

那样尝试

  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