Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript second parameter type based on first parameter

Tags:

typescript

How to force proper second argument type for tthe createMessage method

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

  }


}

so i.e. only objects of type DoctorSignupOutput are allowed when template equals DOCTOR_SIGNUP?

like image 275
Funky coder Avatar asked May 11 '20 16:05

Funky coder


Video Answer


1 Answers

try like that

  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) {

  }

A detailed example:

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

like image 56
satanTime Avatar answered Oct 28 '22 10:10

satanTime