microsoft / microsoft/TypeScript
Add refactoring option to convert between function declaration and function variable
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
Search Terms
- refactor function declaration
- refactor to arrow function
Suggestion
Consider adding refactoring from
const fn = (x: number): number => {
return x + 1
}
into
function fn(x: number): number {
return x + 1
}
and vice-versa.
Use Cases
I found myself doing this refactor by hand many times. I think the main use cases are:
- Project has code style which enforces one of the above, and you're pasting some other code.
- Particular function declaration simply looks better when written as block-less arrow function.
- After refactoring an arrow function to a variable in current scope, it always picks the arrow style, but it could be written as a declaration as well.
Examples
Don't know if viable, but here it goes:
export const Component: React.FC<Props> = props => {
return <h1>Hello, {props.name}</h1>
}
export function Component(props: Props) {
return <h1>Hello, {props.name}</h1>
}
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No files, tests, or entry points are named in the issue. Start by locating the existing refactoring support for function declarations and function variables, then determine how both directions should preserve the shown parameters, return types, exports, and JSX usage. Done means both conversions are available as refactoring actions with coverage for the illustrated TypeScript and TSX cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- developer-experience, devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100