How can I check whether an optional parameter was provided?
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();
}
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