question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add refactoring action to annotate a variable or a function signature component with its inferred type

See original GitHub issue

Hi, I’m using latest stable VSCode / latest nightly TS and I’m not sure if codefixes and refactorings have been implemented there yet, in any case, having a refactoring option to take any variable which receives an implicit inferred type, say, through a function call:

function test(): number[] {
  // ...
}

let x = test(); 

And being able to right-click x and invoke an action like Annotate with inferred type (or Extract inferred type), such that the resulting statement would look like:

let x: number[] = test();

could be very useful!

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:17
  • Comments:15 (6 by maintainers)

github_iconTop GitHub Comments

6reactions
aluanhaddadcommented, Dec 28, 2016

I would certainly appreciate having the opposite capability.

3reactions
rotemdancommented, Dec 29, 2016

Also, being able to annotate functions or methods with their inferred return types, e.g.

function test() {
  return [1,2,3,4];
}

Right click test, select Annotate with inferred return type, leading to:

function test(): number[] {
  return [1,2,3,4];
}

Would also be very useful. I do that a lot, sometimes to have it visibly documented, or make sure that the return type is stabilized such that further changes to the body of the function that break it would be detected.

There are also cases where callback’s signature types are inferred like

function example(f: (a: number, b: string) => boolean) {
}

example((x, y) => {
})

Where right clicking x, y or => and choosing Annotate with inferred type/Annotate with inferred return type or even combined to a single action like Annotate with inferred signature (maybe when, say, the arrow is right clicked for an anonymous function) could also be useful.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The Infer function return type refactoring for TypeScript
Today's VS Code tip: Infer function return typeUse this refactoring to quickly add explicit type annotations to functions /methods in ...
Read more >
TypeScript refactoring with Visual Studio Code
Visual Studio Code has built-in support for TypeScript refactoring through the TypeScript language ... The Infer function return type refactoring in action.
Read more >
My experience with Typescript annotations - Medium
Typescript allows you to add types to JavaScript files using ... just assert the type of that variable and rest is inferred correctly....
Read more >
1. Typechecking - Hack and HHVM [Book] - O'Reilly
To enhance the typechecker's ability to do this, Hack allows programmers to explicitly annotate the types of some values in their programs: function...
Read more >
Refactoring Java Generics by Inferring Wildcards, In Practice
Wildcard annotations can improve the generality of Java generic libraries, but require heavy ... The type signature of List.add contains the type parameter....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found