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

Inconsistent error between different versions of the same function with required parameter following an optional one #57565

Open
Andarist opened this issue Feb 27, 2024 · 3 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@Andarist
Copy link
Contributor

πŸ”Ž Search Terms

error function optional required rest

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.5.0-dev.20240227#code/GYVwdgxgLglg9mABAIwIYCcAUqBciwgC2yApugDQoD8eBxZlEtRpFiAdJxgOYDOevKOhhhuAbQC6ASkQBvAL4AoRWnSKA9OsQA9KolCRYCFBmzN6bZDXwsGiJjYuVO7Hv0SDhoyVLwA3OBgAE2VVTABGKQBuRE1EAFEADwAHEmgSIMRUKEQAGxJUQUQAZiz0biISMCheSmQQHO44HPD2TAAmAFZuqWUDaHgkNAAvM0dWOus6CY4ucvcxB2m7FzcBIRFxCWk5JRVUYY0tXX1wAeMRseXLKds2JbvnOb51ry3fRADg0IOI6NitElUulMtk8gUiqUeJVqrUUA1EE0Wm0uj1FEA

πŸ’» Code

function bar(a: number, b?: number, c: number, ...args: string[]) {}

bar
// ^? function bar(a: number, b?: number, c: number, ...args: string[]): void

bar(1); // Expected at least 3 arguments, but got 1.(2555)

function baz(a: number, b?: number, ...args: [c: number, ...args: string[]]) {}

baz
// ^? function baz(a: number, b?: number, c: number, ...args: string[]): void

baz(1); // Expected at least 3 arguments, but got 1.(2555)

πŸ™ Actual behavior

We get an error within the bar declaration ("'c' is declared but its value is never read.(6133)") but we get none in baz. They are pretty much equivalent.

πŸ™‚ Expected behavior

I'd expect this error to be raised consistently here (or not raised consistently).

In addition to that, b comes with the question mark in both quick infos here. TypeScript understands that at least 3 arguments are required here. It means that effectively b is required in both cases and ? is somewhat misleading here. I think it would be good to normalize this. It already is normalized with tuples:

type A = [a: number, b?: number, ...[c: number, ...args: string[]]]
//   ^? type A = [a: number, b: number | undefined, c: number, ...args: string[]]

Additional information about the issue

No response

@RyanCavanaugh
Copy link
Member

I'm not really seeing it. You'll always be able to get into this state via instantiation. I think the error about "required may not follow optional" is just a sanity check that you wrote the parameter list correctly, not a global invariant that needs to be enforced in all cases.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Feb 28, 2024
@Andarist
Copy link
Contributor Author

Fine by me, I mainly opened this to recheck the intended behavior. What do you think about the small display issue that I mentioned?

In addition to that, b comes with the question mark in both quick infos here. TypeScript understands that at least 3 arguments are required here. It means that effectively b is required in both cases and ? is somewhat misleading here. I think it would be good to normalize this. It already is normalized with tuples:

@RyanCavanaugh
Copy link
Member

I could be convinced either way on display. Either seems justifiable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants