I'm currently working on opening a typescript method that utilizes generics. The scenario involves an object with different methods, each with specified types for function parameters.
const emailTypes = {
confirmEmail: generateConfirmEmailOptions, // { a: string, b: string;}
restorePassword: generateRestorePasswordOptions, // { a: string, b: number;}
};
The type with keys from this object:
export type IEmailTypes = typeof emailTypes;
and here is the method itself:
export const getEmailOptions = <T extends keyof IEmailTypes>(emailType: T, emailData: Parameters<IEmailTypes[T]>[0]) =>
emailTypes[emailType](emailData);
I am encountering an issue specifically with (emailData)
, and I cannot figure out why. I believed I was following the correct approach by passing the key names to T
and then extracting the function parameters based on this key. However, it seems there are discrepancies in types.
My intention is to have TypeScript provide clear guidance on what emailData
should be when inputting, for example, confirmEmail
into emailType
.
If anyone can point out where I might be going wrong, I would greatly appreciate it.
For better clarity, I've included a screenshot.https://i.sstatic.net/imiSA.png
*methods in emailTypes
have parameter types