DEV Community

Cover image for TypeScript Optional Parameters
Johnny Simpson
Johnny Simpson

Posted on • Edited on • Originally published at fjolt.com

TypeScript Optional Parameters

In Javascript, if a function has 4 parameters, and we only use 3, it's no big deal - Javascript just ignores the missing parameter and runs the function as if it is undefined.

In TypeScript, things are a little different. If you try to run a function with a different number of parameters than the function actually has, you'll get an error. For example, consider this:

const myFunction = (a: string, b: string, c?: string) => {
    return a + " " + b + " " + c;
}

let runFunction = myFunction("Hello", "there");
Enter fullscreen mode Exit fullscreen mode

The above code will throw the following error:

Expected 3 arguments, but got 2.
Enter fullscreen mode Exit fullscreen mode

TypeScript does this to ensure fewer errors, since leaving a parameter out of a function you are calling can sometimes break the function. As such, we need to tell TypeScript specifically that one or more of the parameters we defined on the function are optional. In order to do that, we use a ? after the parameter, to denote that it is actually an optional parameter.

For example, if we wanted to make c optional on our original function, we could write the following - and not get an error:

const myFunction = (a: string, b: string, c?: string) => {
    return a + " " + b + " " + c;
}

let runFunction = myFunction("Hello", "there");

console.log(runFunction);
Enter fullscreen mode Exit fullscreen mode

Now our function will run as expected, however the output will be Hello there undefined. This may not be your desired behaviour, which is exactly why TypeScript has these controls in place - it can help prevent unwanted outcomes by requiring certain parameters.

In TypeScript, however, an optional parameter cannot be followed by a required one. So for example, the following code will not work:

const myFunction = (a: string, b?: string, c: string) => {
    return a + " " + b + " " + c;
}

Enter fullscreen mode Exit fullscreen mode

It will produce the following error:

A required parameter cannot follow an optional parameter.
Enter fullscreen mode Exit fullscreen mode

We can combine this optionality with the use of the typeof operator to produce code that makes sense. For example, if c is optional and undefined, then we can ignore it, like so:

const myFunction = (a: string, b: string, c?: string) => {

    if(c === undefined) {
        return a + " " + b;
    }

    return a + " " + b + " " + c;
}

let runFunction = myFunction("Hello", "there");

console.log(runFunction);
Enter fullscreen mode Exit fullscreen mode

Now the output of our code will be Hello there, but we could also run it with c to produce a different output, for example:

const myFunction = (a: string, b: string, c?: string) => {

    if(c === undefined) {
        return a + " " + b;
    }

    return a + " " + b + " " + c;
}

let runFunction = myFunction("How", "are", "you?");

// Returns "How are you?"
console.log(runFunction);
Enter fullscreen mode Exit fullscreen mode

Top comments (5)

Collapse
 
ecyrbe profile image
ecyrbe • Edited

You don't need to do a runtime typeof JavaScript introspection. Indeed, here c is declared. Use typeof only to check if a variable is declared.

if ( c === undefined )
Enter fullscreen mode Exit fullscreen mode

Just works in your example.

You can use typeof to check for global variables. For example :

window === undefined
Enter fullscreen mode Exit fullscreen mode

will throw an exception on nodejs when it will return false on your browser.
In this case, yes you should use typeof:

typeof window === "undefined"
Enter fullscreen mode Exit fullscreen mode
Collapse
 
smpnjn profile image
Johnny Simpson

You're right, in fact typeof c == undefined won't work at all since typeof returns a string. I've updated.

Collapse
 
peerreynders profile image
peerreynders

Default values?

If you have multiple optional parameters you can force the default value of the argument(s) you don't want to explicitly provide by passing undefined (no, null doesn't work; drives home that undefined is the real bottom type).

Collapse
 
smpnjn profile image
Johnny Simpson • Edited

you can do that, yes, it's a good point. Maybe I can write something else about default values.

Collapse
 
omarbenmegdoul profile image
Omar Benmegdoul

Am I missing something or is there a typo? You've marked c as optional in the first two snippets