Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Declaring multiple arguments configurations of a function in typescript

I have a function that can accept one parameter and it can have multiple types.

    getItems(user: User | null = null) {
        if (user instanceof User) return getItemsOfUser(user);
        getAllItems();
    }

In order to increase readability I wonder if it is possible to have a notation like this one:

    getItems(user: null)
    getItems(user: User) {
        if (user instanceof User) return getItemsOfUser(user);
        getAllItems();
    }

The logic in this layer is supposed to receive a generic case and call the more specific service functions.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Yes, you are seeking function overload
See : how-to-overload-functions

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading