Skip to content

Refactorings to convert from function declarations to function expressions #44080

Open
@OliverJAsh

Description

@OliverJAsh

Suggestion

🔍 Search Terms

refactoring quick fix vs code action

✅ Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

We already have refactorings to convert function expressions (arrow or non-arrow) to a function declaration, e.g. we can use "convert to named function" on both of these:

const fn1 = function () {
    return 1;
};

const fn2 = () => {
    return 1;
};

However, there is no refactoring to convert in the opposite direction—from a function declaration to a function expression.

I would like to suggest adding these refactorings so we cover all possibilities.

Note we do already have these refactorings (#28250):

  • "Convert to arrow function"
  • "Convert to anonymous function"

However these only work on function expressions—they don't work on function declarations. Perhaps we could fix this by extending the scope of those refactorings so they do work on function declarations.

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 featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions