Skip to content
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

No TS2554 in JavaScript files even though the argument is definitely required. #50331

Open
jespertheend opened this issue Aug 17, 2022 · 2 comments
Labels
Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript

Comments

@jespertheend
Copy link
Contributor

Bug Report

πŸ”Ž Search Terms

javascript TS2554

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about required

⏯ Playground Link

πŸ’» Code

function requiredArgument({
    foo = true,
}) {
    console.log(foo);
}

requiredArgument();

πŸ™ Actual behavior

No type error when calling requiredArgument()

πŸ™‚ Expected behavior

A type error saying Expected 1 arguments, but got 0. TS2554, since this is also the current behaviour in TypeScript files.
And when running the JavaScript code, you get a runtime error:

Uncaught TypeError: Cannot read properties of undefined (reading 'foo')

Related: #49869

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. labels Aug 17, 2022
@RyanCavanaugh
Copy link
Member

Not really clear what to do about this. Destructuring is considered an implementation detail of the function, i.e. it's considered the same as its desugaring

function requiredArgument(obj) {
    const foo = obj.foo;
    console.log(foo);
}

and parameters in JavaScript are never considered required (since there's no syntactic clue either way).

You can mark this required with an @param tag.

/**
 * @param _
 */
function requiredArgument({
    foo,
}) {
    console.log(foo);
}

// Errors
requiredArgument();

@jespertheend
Copy link
Contributor Author

Would it make sense to use the lack of = {} as a syntactic clue that the parameter is required?
After all parameter types are already inferred where possible. For example in the case above:

function requiredArgument({
    foo = true,
}) {
    console.log(foo);
}

the first parameter is already inferred to have the type:

{
    foo: true;
}

In fact if you hover over the function the reported signature is already being inferred to be required.
image
This reported signature is the same in both TypeScript and JavaScript, therefore it seems weird to me that adding JSDoc to the function causes behaviour to be different when calling it. Because the reported signature is the same regardless of the JSDoc tag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants