The goal: Make the code editor get the expected types definitions while I'm coding
Last active
March 16, 2022 14:30
-
-
Save erkobridee/71d32c44bc9fe725a4658efecb16007c to your computer and use it in GitHub Desktop.
simple example of how to define functions overloads on typescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: '[email protected]' }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: '[email protected]' } }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment