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

Generic inference from LHS of assignment when destructuring can lead to unexpected anys #45074

Closed
jkillian opened this issue Jul 16, 2021 · 5 comments Β· Fixed by #49086
Closed
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Rescheduled This issue was previously scheduled to an earlier milestone

Comments

@jkillian
Copy link

Bug Report

πŸ”Ž Search Terms

Generic inference, destructuring, binding patterns

πŸ•— Version & Regression Information

Exists in 4.3.5

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

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

declare function genericFunc<T>(): T;
const { bar } = genericFunc();
// `bar` is `any` here.

πŸ™ Actual behavior

bar is any and T is inferred to be { bar: any }.

πŸ™‚ Expected behavior

The inferred type of T should be unknown and there should be an error from trying to destructure it.
(Or, T could still be inferred as { bar: any } but then noImplicitAny should trigger an error. This seems at a glance less desirable to me than the former, but I don't know enough how generic inference works to make a more informed recommendation. Anyways, some sort of error no matter how it happens is what would be ideal!)

Notes: see #43371 (comment) for the original context.

@andrewbranch
Copy link
Member

The inference of { bar: any } may or may not be avoidable without breaking a lot of other inferences, but it should at least error under noImplicitAny.

@andrewbranch andrewbranch added this to the TypeScript 4.5.0 milestone Jul 21, 2021
@andrewbranch andrewbranch self-assigned this Jul 21, 2021
@andrewbranch
Copy link
Member

Related: #43605

@jkillian
Copy link
Author

jkillian commented Oct 1, 2021

Related to the discussion in #46012 I believe

@andrewbranch
Copy link
Member

Yeah. Probably not going to fix the noImplicitAny error for 4.5 because I want to investigate making this whole pattern an error in the 4.6 timeframe, as discussed in that design meeting.

@andrewbranch andrewbranch added the Rescheduled This issue was previously scheduled to an earlier milestone label Dec 7, 2021
@typescript-bot typescript-bot added Fix Available A PR has been opened for this issue labels May 12, 2022
@jkillian
Copy link
Author

Love that this is fixed, thanks @andrewbranch!

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 Rescheduled This issue was previously scheduled to an earlier milestone
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants