How can I check whether an optional parameter was provided?

Typescript

Typescript Problem Overview


Given a function with optional parameters:

function DoSomething(a, b?) {
	/** Implementation */
}

How can I determine whether an optional parameter was provided from within the function body? Currently, the best way to do this that I can think of is:

typeof b === 'undefined'

But this is kind of messy and not straight-forward to read. Since TypeScript provides optional parameter support, I'm hoping it also has an intuitive way to check if a parameter was provided.

As the above example shows, I don't mind whether the optional parameter was explicitly set to undefined or not supplied at all.

Edit

Unfortunately, this question wasn't as clear as it should have been, particularly if it's skim-read. It was meant to be about how to cleanly check if an optional parameter is completely omitted, as in:

DoSomething("some value");

I've accepted Evan's answer since his solution (b === undefined) is cleaner than the one in my question (typeof b === 'undefined') while still having the same behaviour.

The other answers are definitely useful, and which answer is correct for you depends on your use case.

Typescript Solutions


Solution 1 - Typescript

After googling "typescript check for undefined", I saw this question at the top of the results, but the answer given by Evan Trimboli did not solve my problem.

Here is the answer that ultimately solved my problem. The following code is what I settled on. It should work in cases where the value equals null or undefined:

function DoSomething(a, b?) {
    if (b == null) doSomething();
}

Solution 2 - Typescript

You can just check the value to see if it's undefined:

var fn = function(a) {
    console.log(a === undefined);
};
    
fn();          // true
fn(undefined); // true
fn(null);      // false
fn('foo');     // false

Solution 3 - Typescript

TypeScript's util module has function isUndefined(). You can use it like this.

import {isUndefined} from "util";

class A {
    test(b?: string): string {
        if (isUndefined(b)) {
            return "UNDEFINED";
        } else {
            return ("DEFINED" + b);
        }
    }
}

Solution 4 - Typescript

you could simple add an optional parameter with an default value like this

function DoSomething(a, b: boolean=null) {
    if(b == null)
    {
      //parameter was not set
    }
    else
    {
    //parameter is true or false...no other option available
    }
}

Solution 5 - Typescript

From here works without problems:

function getSchool(name: string, address?: string, pinCode?: string): string {
    if (typeof address === 'undefined') {
        console.log('address not defined');
    }
    if (typeof pinCode === 'undefined') {
        console.log('pincode not defined');
    }
    //...
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSamView Question on Stackoverflow
Solution 1 - TypescriptTod BirdsallView Answer on Stackoverflow
Solution 2 - TypescriptEvan TrimboliView Answer on Stackoverflow
Solution 3 - TypescriptVladimir PrudnikovView Answer on Stackoverflow
Solution 4 - TypescriptTobias KollerView Answer on Stackoverflow
Solution 5 - TypescriptPavel_KView Answer on Stackoverflow