Skip to content

Optional properties of function parameter not detected when default parameter created indirectly #43725

Closed
@dummdidumm

Description

@dummdidumm

Bug Report

When calling a function with an object which has a default parameter and is destructured with fallback values, optional properties are only inferred correctly if that default parameter is created ad-hoc. If it's a function call or variable reference, the optional properties of the parameter are no longer detected.

🔎 Search Terms

function default parameter optional

🕗 Version & Regression Information

  • This is the behavior in every version I tried (3.3.3 to Nightly), and I reviewed the FAQ for entries about function calls

⏯ Playground Link

https://www.typescriptlang.org/play?ts=4.2.3&ssl=13&ssc=1&pln=1&pc=1#code/GYVwdgxgLglg9mABAdzgJwNYGcAUBvRAQwBpEAjRAXkSjRAFNEBfKxAwgLhrvtLK9oNmASjZMAUKky48nbgybCA3OPGhIsBIgAm9YIRAAbKAAVCaQgFsAjDlF5xiJ4jT0oINEnYCefH0KYVCXVoeCRtOHosMCgAdXQMW3Y+VkFGFmpdfSNTcytbYXsJCKiY+MwkuTTFJUQAejryFDQEAHNQQ0MATxd6AEcQGFdtVQgELCgdPQNjMwtLACZWb3lecn90lRDNcMjouISF-CIU6jTmViyZ3PmFovES-fKMI9kNmvrGimQWsHajbq9AZDegjIA

💻 Code

function works({ a, b = true } = { a: true, b: true }) {}
works({a: true});

function defaultParam1() {
    return { a: true, b: true };
}
function doesntWork1({ a, b = true } = defaultParam1()) {}
doesntWork1({a: true}); // b wrongfully required

const defaultParam2 = { a: true, b: true };
function doesntWork2({ a, b = true } = defaultParam2) {}
doesntWork2({a: true}); // b wrongfully required

🙁 Actual behavior

Destructuring a function parameter with a default value doesn't mark properties as optional that have a fallback value when the default parameter isn't created ad-hoc.

🙂 Expected behavior

It doesn't make a difference if the default parameter is created ad-hoc or indirectly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions