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

Parameter contextually typed from rest parameters loses possibility of undefined #46063

Open
uhyo opened this issue Sep 26, 2021 · 3 comments
Open
Labels
Bug A bug in TypeScript
Milestone

Comments

@uhyo
Copy link
Contributor

uhyo commented Sep 26, 2021

Bug Report

🔎 Search Terms

function parameter contextual contextually rest undefined

🕗 Version & Regression Information

3.3.3333, 4.5.0-dev.20210925

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

⏯ Playground Link

Playground link with relevant code

💻 Code

type Func = (...args: number[]) => void

const func: Func = (arg) => {
    console.log(arg.toFixed(3));
}

func();

🙁 Actual behavior

(Under strict: true, and also noUncheckedIndexedAccess: true as a bonus)

The type of arg is inferred as number although undefined could flow in.

🙂 Expected behavior

The type of arg should be number | undefined.

⛰️ Context

Related: #45972

I was thinking of a simpler setting and found that handling of normal rest parameters isn't perfect. These two are not the same issue given that this one doesn't result in a compile error.

@fatcerberus
Copy link

Technically that arrow function shouldn’t be assignable to Func at all since Func can legally be passed zero arguments but arg is required. Seems like an oversight in function assignability checking.

@uhyo
Copy link
Contributor Author

uhyo commented Sep 29, 2021

Personally I think TypeScript could allow receiving parameters that may not exist from rest parameters, similarly to how TypeScript currently allows accessing array elements that may not exist. Considering noUncheckedIndexedAccess in that case may be nicer.

Either way, current behavior is unsafe, so a fix is desired.

@andrewbranch andrewbranch added the Bug A bug in TypeScript label Oct 1, 2021
@andrewbranch andrewbranch added this to the Backlog milestone Oct 1, 2021
@Andarist
Copy link
Contributor

Just stumbled upon this and I second the @uhyo here. I think that an added | undefined would be slightly better than an assignability error. It would make it easier to handle more crazy scenarios with the "source" rest parameter being a union of tuple types (potentially with different lengths etc).

It wouldn't introduce any unsoundness. After all, the declared function could still be called soundly - it would just affect the types visible in the implementation of this function and that would increase the type safety

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants