-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a refactoring that annotates an implicit or explicit any with a best guess type inferred from its use sites #13243
Comments
but why (might have been |
Perhaps he wants function log(x: any) {
console.log(x.bar.baz);
} => function log(x: {bar: {baz: any}}) {
console.log(x.bar.baz);
} That said the original suggestion is easily done in most editors with I do this quite commonly myself as well to cleanup existing JS code to discover how the argument is actually used 🌹 |
@basarat what is the competitive advantage of atom compared to vscode? |
@Aleksey-Bykov |
I forgot I wrote the reasons : https://basarat.gitbooks.io/alm/content/contributing/why.html . Been a while since someone has asked :) |
I think it would be a good idea for tslint |
I apologize for not providing more context and not explaining my primary use case. As we know, Implicit In some scenarios, typically function declarations, we know what the type is but the compiler has not inferred it. In such cases it is only natural to annotate it with that the type we know it should be, e.g. However, in other scenarios we do not yet know, do not yet care, or have not yet decided what the type should be. In such cases it is tempting to explicitly annotate with function log(x: any) {
console.log(value); // seems fine.
} Now assume we decide that we want to log the function log(x: any) {
console.log(x.name);
} In the above code, our function has changed its requirements but will continue to accept values that do not have a Due to the viral nature of If Having a refactoring would encourage this.
@basarat indeed that is the spirit of this suggestion. That would be a great feature.
@Aleksey-Bykov indeed. I want less not more. |
aside from you usecase which makes sense, furthermore it's rather trivial to decontaminate your code from
|
That is a great point. Indeed you have clarified that what I actually want is not
@xLama Suggested linting as well. Perhaps that is where this idea belongs. |
i gave it a second thought, i might be helpful if such code action looked for all places that call this function and tried to deduce the replacement type out of all possible arguments out there
|
I have renamed this issue to reflect the far more useful refactoring proposed by @Aleksey-Bykov in his remarks above. The proposed refactoring would look at how a value implicitly typed as |
Addressed by #14786 |
Can this issue be closed? It sounds like it's been addressed. |
nope. i need to update #14786, get it reviewed and merged. |
Still super excited about this feature. Thanks @mhegazy |
I just read #14786, and am also incredibly excited to start using this feature! Out of curiosity, any reason not to use the same feature for implicitly inferring function parameters rather than assuming any unannotated inputs are type |
Sorry, just noticed #15114 and all its friends that discuss that very issue. I'm quite happy to see this Quick Fix solution after reading through many of the problems with function parameter inference! |
I would very much appreciate having a codefix (refactoring) that, given an explicit
any
annotation, offers to change it to an{}
annotation.For example, given
a code fix would be offered to change the declared type of
x
to{}
resulting inHowever, given
a code fix would be offered to change the declared type of
x
to{ name: any }
resulting inThe text was updated successfully, but these errors were encountered: