Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dummdidumm opened this issue Apr 18, 2021 · 2 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@dummdidumm
Copy link

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.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Apr 19, 2021
@RyanCavanaugh
Copy link
Member

This is the intended behavior. We always use the type of the parameter default to determine the type of the parameter; destructuring defaults are considered "part of the function implementation" and not used to influence the type of the parameter. What you see here is that the type of the fresh object literal in the works example gets contextually typed by the binding pattern and thus acquires a different type than it has a non-contextually-typed position like in defaultParam2.

@dummdidumm
Copy link
Author

Thanks for the explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

2 participants