Skip to content

Instantly share code, notes, and snippets.

@erkobridee
Last active March 16, 2022 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erkobridee/71d32c44bc9fe725a4658efecb16007c to your computer and use it in GitHub Desktop.
Save erkobridee/71d32c44bc9fe725a4658efecb16007c to your computer and use it in GitHub Desktop.
simple example of how to define functions overloads on typescript
enum ETemplate {
HELLO_WORLD = 'hellow_world',
CONTRACT = 'contract',
REPORT = 'report',
EMAIL = 'email'
}
type TTemplates = `${ETemplate}`;
interface ITemplateBase<T = TTemplates> {
templateId: T;
}
interface IHelloWorldTemplate extends ITemplateBase<ETemplate.HELLO_WORLD> {
greetings: string;
}
interface IContractTemplate extends ITemplateBase<ETemplate.CONTRACT> {
contractNumber: number;
}
interface IReportTemplate extends ITemplateBase<ETemplate.REPORT> {
value: number;
}
interface IEmailTemplate extends ITemplateBase<ETemplate.EMAIL> {
email: string;
}
//------------------------------------------------------------------------------------------------//
function renderTemplate(template: IHelloWorldTemplate): void;
function renderTemplate(template: IContractTemplate): void;
function renderTemplate(template: IReportTemplate): void;
function renderTemplate(template: IEmailTemplate): void;
function renderTemplate(template: any) {
console.log('do the job', { template });
}
renderTemplate({ templateId: ETemplate.HELLO_WORLD, greetings: "greetings to the eathlings" });
renderTemplate({ templateId: ETemplate.CONTRACT, contractNumber: 123 });
renderTemplate({ templateId: ETemplate.REPORT, value: 1000 });
renderTemplate({ templateId: ETemplate.EMAIL, email: 'contact@domain.ext' });
enum ETemplate {
HELLO_WORLD = 'hellow_world',
CONTRACT = 'contract',
REPORT = 'report',
EMAIL = 'email'
}
type TTemplates = `${ETemplate}`;
interface ITemplate<T = TTemplates, D = unknown> {
templateId: T;
data: D;
}
interface IHelloWorld {
greetings: string;
}
interface IContract {
contractNumber: number;
}
interface IReport {
value: number;
}
interface IEmail {
email: string;
}
//------------------------------------------------------------------------------------------------//
function renderTemplate(template: ITemplate<ETemplate.HELLO_WORLD, IHelloWorld>): void;
function renderTemplate(template: ITemplate<ETemplate.CONTRACT, IContract>): void;
function renderTemplate(template: ITemplate<ETemplate.REPORT, IReport>): void;
function renderTemplate(template: ITemplate<ETemplate.EMAIL, IEmail>): void;
function renderTemplate(template: any) {
console.log('do the job', { template });
}
renderTemplate({ templateId: ETemplate.HELLO_WORLD, data: { greetings: "greetings to the eathlings" } });
renderTemplate({ templateId: ETemplate.CONTRACT, data: { contractNumber: 123 } });
renderTemplate({ templateId: ETemplate.REPORT, data: { value: 1000 } });
renderTemplate({ templateId: ETemplate.EMAIL, data: { email: 'contact@domain.ext' } });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment