Typescript - Overloading

About

Function - Overloaded Function (Overloaded) in typescript

Method

Union Type

You can declare two possible type for a variable

Example when you want to store a object

export interface user {
    name: string
}

/**
* You may get the object or a json string
*/
function set(user: string | user) {
    let nameS: string;
    if (typeof user != 'string'){
        userS = JSON.stringify(user);
    } else {
        userS = user;
    }
    localStorage.setItem(localStorageKey, userS);
}







Share this page:
Follow us:
Task Runner