4

i just noticed , that this function (using default params) doesn't cause error on compilation.

function buildAddress(address1 = 'N/A', address2: string) {
        displayAddress( address1 +' '+ address2);
    }

but this function (using optional params) does.

function buildAddress(address1?: string, address2: string) {
        displayAddress( address1 +' '+ address2);
    }

why is it so ?

i'm really surprised of this behavior, is that normal ? does it have any benefit ? is this a feature or a bug ?

1 Answer 1

6

Have you tried to use the first version without passing the first argument?

function buildAddress(address1: string = 'N/A', address2: string) {
    console.log(address1, address2);
}

buildAddress("address2");

Results in:

Supplied parameters do not match any signature of call target

If you put the default value for the 2nd parameter though:

function buildAddress(address1: string , address2: string = 'N/A') {
    console.log(address1, address2);
}

It works.

Adding default value for the first parameter only helps you in case you pass undefined:

buildAddress(undefined, "address2");

As it compiles to:

function buildAddress(address1, address2) {
    if (address1 === void 0) { address1 = 'N/A'; }
    console.log(address1, address2);
}

So in reality if you're doing this then the first parameter isn't optional at all, you must pass a value, and you only get the default value if you pass undefined.
But the compiler won't complain about the function signature because the first parameter has a value for sure, but in your 2nd function, since the first param is optional the compiler complains.


Edit

This behvior can be used to safe guard against undefined values, for example:

function buildAddress(address1 = 'N/A', address2: string) {
    displayAddress(address1 + ' ' + address2);
}

function getAddress1(): string {
    // logic here, might return undefined
}

buildAddress(getAddress1(), "address 2");

I'm not sure if this is by design or just a byproduct, but it's useful in certain cases.

Sign up to request clarification or add additional context in comments.

2 Comments

yea i know that, but i'm really surprised of this behavior, is that normal ? does it have any benefit ? is this a feature or a bug ?
I don't know if it's a bug, but I edited my answer with an example of how it's useful. You can file a bug report and see how they respond. I would like to follow on that if you do

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.