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 error in JavaScript on optional binding pattern parameter #49869

Closed
jespertheend opened this issue Jul 12, 2022 · 5 comments Β· Fixed by #50094
Closed

No error in JavaScript on optional binding pattern parameter #49869

jespertheend opened this issue Jul 12, 2022 · 5 comments Β· Fixed by #50094
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Good First Issue Well scoped, documented and has the green light

Comments

@jespertheend
Copy link
Contributor

jespertheend commented Jul 12, 2022

Bug Report

πŸ”Ž Search Terms

TS2463 javascript

πŸ•— Version & Regression Information

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

⏯ Playground Link

πŸ’» Code

/**
 * @param [options]
 */
function test({ x = '' }) { x }

πŸ™ Actual behavior

No error on { x = '' }.

πŸ™‚ Expected behavior

Error 2463: "A binding pattern parameter cannot be optional in an implementation signature."

The equivalent Typescript has the correct error:

function test({ x = '' }?) { x }

Note that in the playground link I have checked all options that are supposed to make types more strict. Also note that when this code is run, you'll end up with a runtime error. The same is true for the TypeScript equivalent, but in that case you get an error in advance warning you that the code you wrote will cause issues.

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Jul 12, 2022
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.9.0 milestone Jul 12, 2022
@sandersn sandersn added Working as Intended The behavior described is the intended behavior; this is not a bug and removed Needs Investigation This issue needs a team member to investigate its status. labels Jul 19, 2022
@sandersn
Copy link
Member

@param {Options} [options] declares the parameter as optional. The equivalent in typescript is { maybeUndefined = 'str' }?: options, which also gets rid of the error. (Although it adds an error at the declaration site since this combination isn't legal -- and should probably be illegal in javascript too.)

Instead, use @param {Options} options

@jespertheend
Copy link
Contributor Author

Thanks for looking into this @sandersn!
I'm not really sure why this got closed as working as intended, since you mentioned

and should probably be illegal in javascript too.

Should I create a feature request / new issue for this?

@sandersn sandersn changed the title No TS2463 in JavaScript for optional parameters in an options object. No error in JavaScript on optional binding pattern parameter Jul 20, 2022
@sandersn
Copy link
Member

Oh, I misread the whole thing. I'll re-open.

@sandersn sandersn reopened this Jul 20, 2022
@sandersn sandersn added Bug A bug in TypeScript Good First Issue Well scoped, documented and has the green light and removed Working as Intended The behavior described is the intended behavior; this is not a bug labels Jul 20, 2022
@JE-lee
Copy link

JE-lee commented Jul 23, 2022

I supposed that it's working as intended.
function test({ x = '' }) { x } means that the first argument is required.
You'll get the error of missing arguments when you attempt to call this test().

@jespertheend
Copy link
Contributor Author

That's true, but with

/**
 * @param [options]
 */

added to the JavaScript function you can suppress type errors that would otherwise warn you about the potential runtime error.
Have a look at the two playground links in the first comment. The emitted code from both JavaScript and TypeScript are the same, and both trigger a runtime error, but the JavaScript equivalent doesn't warn you in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Good First Issue Well scoped, documented and has the green light
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants