1

Let's have TypeScript function for sending emails:

enum ETemplate {
  EXAMPLE1, EXAMPLE2
}

function sendEmail(to: string, template: ETemplate, params: IParams): Promise<void>{
  await this.mailService.send(to, template, params);
}

interface IParams {
  // depends on template type (ETemplate)
}

How to require params depending on template type I entered? Reason why is because every template have different IParams based on ETemplate.

I know TypeScript have generics but I am not sure if I can achieve this with them...

1 Answer 1

2

You don't need to use generics in this case. Use discriminated unions instead:

enum TemplateType {
    Template1,
    Template2
}

type Template =
    | { type: TemplateType.Template1, params: { aaa: number } }
    | { type: TemplateType.Template2, params: { bbb: string, ccc?: boolean } };

const sendEmail = async (to: string, template: Template): Promise<void> => {
    if (template.type === TemplateType.Template1) {
        template.params.aaa; // number

        return;
    }

    if (template.type === TemplateType.Template2) {
        template.params.bbb; // string
        template.params.ccc; // boolean | undefined

        return;
    }

    template; // never
}


sendEmail("[email protected]", { type: TemplateType.Template1, params: { aaa: 123 } }); // OK
sendEmail("[email protected]", { type: TemplateType.Template2, params: { aaa: 123 } }); // Error as expected
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.