microsoft / microsoft/TypeScript

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

Open
#57,565 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied TypeScript Playground example and compare the diagnostics for bar and baz, including their quick-info signatures. Done means deciding whether the unused-parameter diagnostic and optional-parameter normalization should be consistent, then adding or updating compiler coverage for the chosen behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.