-
-
Notifications
You must be signed in to change notification settings - Fork 6
Use inferred types for function parameters in typescript #3
Comments
Agreed, that would be great! I also think @fabien0102 would love to work on that with me 😉 I didn't know how to get the types of Identifiers from the extension code. We might want to dig into VS Code LSP: https://code.visualstudio.com/api/language-extensions/language-server-extension-guide |
Potential leads:
I tried a POC with @typescript/vfs but I didn't managed to get the inferred types yet: https://gist.github.com/nicoespeon/fb725f1f51e0b506cbfd3a1269efeff1 |
Here we go, I've introduced a const typeChecker = new TypeChecker("const hello = [1];");
typeChecker.getTypeAt(new Position(0, 7));
// => number[] Here it is: https://github.com/nicoespeon/hocus-pocus/blob/master/src/create-function-with-types/type-checker.ts My plan is to introduce a new operation: Then I'll expose This is necessary to avoid overloading the regular |
Very nice! |
Thank you thank you thank you 👍 This is so useful :-) |
@automatensalat we've something working! Check it out: I'll release it in a new version today, so you can start using it and let me know if anything is off. |
Fantastic, I'll check it out! Thank you very much. |
I'm having some issues: If I try to run it on the example I get this error:
If I run it on this: function testCreateFuncWithTypes() {
const a = 3;
doSomething(a);
} it creates function doSomething(a: 3) {
// Implement
} Instead of the type |
@automatensalat the Now, that's the TS theory. I can definitely see that in real-life, we set I suggest we improve the type-checker to do that on top of TS response. As for the object, I'm not sure. It might be the same issue. 🤔 Can you create another issue with a few real-life examples to handle? That would give us a baseline of tests to make pass 💪 |
I can get behind the narrowed type argument, however I think the general types would be more useful in practice. |
Thanks for the issues, that's perfect. And yeah, I think we can improve the Type Checker to make usage the most convenient. General types will probably be more useful. |
Hey @nicoespeon, thanks for the recommendation, this extension is what I was looking for.
Describe what's missing
When using Typescript and invoking the "extract function" feature I'd like the generated code to use the declared or inferred Typescript types for function parameters and return types if possible.
How would it work
The extension would use some internal system which provides the Typescript types and use those when generating declaring the function declaration.
Expected behavior
E.g. if I have this code
I'd like the generated code to be
Additional information
The text was updated successfully, but these errors were encountered: