Description
Search Terms
- Refactoring, refactor
- type annotation
- quick fix, code action
- function, method
- return
Suggestion
Add a refactoring that takes a function with no annotated return type, and inserts the inferred return type.
For example, starting with the code:
function foo() {
return { a: 1, b: 2 };
}
If you trigger the refactoring on foo
, it would become:
function foo(): { a: number; b: number; } {
return { a: 1, b: 2 };
}
Use Cases
A few example use cases:
-
Give your public interfaces explicit typings.
-
Updating a function from
() => T
to() => T | undefined
. Today, you have to explicitly type out the return type and then add| undefined
-
Extracting the return type to a new interface. Right now, this is a manual process. With this refactoring, this becomes a two step process: generate inferred return type and then extract to interface
-
When migrating from JS to TS, many JS functions may not have explicit return types. Often you want to make the code stricter by having these return types