TypeScript – Method Overloading

export class HtmlElementFactory {
// method overloading – sort off… pretty weird
static CreateGeneric(elementName: string): HTMLElement;
static CreateGeneric(elementName: string, elementId: string,
innerHTML:string,attributes: Array<HtmlElementAttributes>) : HTMLElement;
static CreateGeneric(elementName: string, elementId?: string,
innerHTML?:string, attributes?: Array<HtmlElementAttributes>) : HTMLElement {
var htmlElement = document.createElement(elementName);
htmlElement.id = (elementId) ? elementId : "";
htmlElement.innerHTML = (innerHTML) ? innerHTML : "";
if (attributes) {
for (let attr of attributes)
htmlElement.setAttribute(attr.key, attr.value);
return htmlElement;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s