GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Suggestion: extract to function expression · Issue #31860 · microsoft/TypeScript

Suggestion: extract to function expression #31860

New issue
Copy link
New issue
Copy link
Open
Open
Suggestion: extract to function expression#31860
Copy link
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScriptAn idea for TypeScript

Description

@OliverJAsh
Oliver Joseph Ash (OliverJAsh)
opened on Jun 11, 2019
Issue body actions

Search Terms

function expression declaration extract method refactoring code fixes

Suggestion

"Extract to function" is very useful, but it only supports generating function declarations. Some people (such as me) prefer to use function expressions instead of declarations.

For example:

const fn = () => {
    const foo = 1;
    const bar = foo + 2
}

If I select line 3 and extract to function, I get

const fn = () => {
    const foo = 1;
    const bar = newFunction(foo)
}

function newFunction(foo: number) {
    return foo + 2;
}

I want:

const newFunction = (foo: number) => foo + 2;

const fn = () => {
    const foo = 1;
    const bar = newFunction(foo)
}

Some ideas:

  • An option to toggle between expressions/declarations
  • Separate refactorings for both expressions and declarations

Use Cases

Examples

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. Ryan Cavanaugh (RyanCavanaugh)
    added
    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this feature
    SuggestionAn idea for TypeScriptAn idea for TypeScript
    on Jun 17, 2019
  2. ypresto commented on Aug 4, 2019

    @ypresto
    Yuya Tanaka (ypresto)
    on Aug 4, 2019
    Contributor
    More actions

    For me:

    • Inner (not-top-level) function?
      • Yes: const clickHandler = (e: Event) => { ... }
      • No: Need to annotate with predefined function type?
        • Yes: const Component: React.FC = () => { ... }
        • No: function someExpensiveLogic() { ... }
  3. ypresto commented on Aug 4, 2019

    @ypresto
    Yuya Tanaka (ypresto)
    on Aug 4, 2019
    Contributor
    More actions

    I'm afraid that too many choices of refactoring would be shown.

    I prefer arrow function for inner function, as it's this binding is just for that. (Currently function declaration is generated.)

    For top level functions, I vote on function declaration. But it might be helpful to have "convert to arrow function" refactoring.

  4. OliverJAsh commented on Jun 30, 2020

    @OliverJAsh
    Oliver Joseph Ash (OliverJAsh)
    on Jun 30, 2020
    ContributorAuthor
    More actions

    But it might be helpful to have "convert to arrow function" refactoring.

    Here's a start nicoespeon/abracadabra#114

  5. OliverJAsh commented on Dec 29, 2020

    @OliverJAsh
    Oliver Joseph Ash (OliverJAsh)
    on Dec 29, 2020
    ContributorAuthor
    More actions

    This idea also applies to the forthcoming "declare function" code fix: #41215. That is, we need a way to let users choose what type of function they want TS to create for them: an expression or a declaration. /cc Oleksandr Tarasiuk (@a-tarasyuk)

    Reacted by Oleksandr Tarasiuk
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

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScriptAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.