镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Closed

Description

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.

Activity

  1. OliverJAsh commented on May 20, 2021

    @OliverJAsh
    ContributorAuthor

    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.

  2. schickling commented on Jun 3, 2021

    @schickling

    Thanks for pushing this forward Oliver Joseph Ash (@OliverJAsh). Really looking forward to this landing at some point! 🙏

  3. OliverJAsh commented on Nov 29, 2021

    @OliverJAsh
    ContributorAuthor

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

  4. added
    CommittedThe team has roadmapped this issue
    and removed
    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this feature
    on Nov 29, 2021
  5. RyanCavanaugh commented on Nov 29, 2021

    @RyanCavanaugh
    Member

    The React scenario makes a lot of sense; sure

  6. OliverJAsh commented on Nov 29, 2021

    @OliverJAsh
    ContributorAuthor

    Thanks, PR here: #46945

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    CommittedThe team has roadmapped this issueSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions