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

"Convert parameters to destructured object": enable for functions with just one parameter #41753

Closed
5 tasks done
OliverJAsh opened this issue Dec 1, 2020 · 5 comments · Fixed by #46945
Closed
5 tasks done
Labels
Committed The team has roadmapped this issue Suggestion An idea for TypeScript
Milestone

Comments

@OliverJAsh
Copy link
Contributor

Search Terms

Suggestion

Back when this refactor was added in #30089, it was decided that the refactor would not be available for functions with less than 2 parameters.

The refactor will not be available on:

  • functions with less than 2 parameters

#30089 (comment)

I would like to suggest that this refactor should be available for functions with just one parameter.

Use Cases

The reason is because I often use this refactoring to make a function into a React component (which always receive their parameters aka props as an object), and it's not infrequent that I need to do this for a function that receives just one parameter.

Furthermore, it would be useful to run this refactoring if you plan to add more parameters immediately afterwards—as opposed to adding more parameters and then converting to an object.

Examples

declare const add: (n1: number, n2: number) => number;

const fn = (a: number) => add(a, 1);

should convert to

declare const add: (n1: number, n2: number) => number;

const fn = ({ a }: { a: number }) => add(a, 1);

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@RyanCavanaugh RyanCavanaugh added 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 labels Dec 3, 2020
@OliverJAsh
Copy link
Contributor Author

I can see from the code that this would be a very easy change: we just need to tweak this constant:

I can go ahead and do that but I guess I need approval from the TS team beforehand. Is there any reason why we wouldn't want to do this? One potential downside is the refactoring will appear more frequently than it currently does, which might annoy some users.

@schickling
Copy link

Thanks for pushing this forward @OliverJAsh. Really looking forward to this landing at some point! 🙏

@OliverJAsh
Copy link
Contributor Author

@RyanCavanaugh I would like to raise a PR to fix this, can you accept this issue and add it to the backlog?

@RyanCavanaugh RyanCavanaugh added Committed The team has roadmapped this issue and removed Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Nov 29, 2021
@RyanCavanaugh
Copy link
Member

The React scenario makes a lot of sense; sure

@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Nov 29, 2021
OliverJAsh added a commit to OliverJAsh/TypeScript that referenced this issue Nov 29, 2021
OliverJAsh added a commit to OliverJAsh/TypeScript that referenced this issue Nov 29, 2021
@OliverJAsh
Copy link
Contributor Author

Thanks, PR here: #46945

sandersn pushed a commit that referenced this issue Feb 3, 2022
…h just one parameter (#46945)

* "Convert parameters to destructured object": enable for functions with just one parameter

Fixes #41753

* Add test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Committed The team has roadmapped this issue Suggestion An idea for TypeScript
Projects
None yet
3 participants