-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Include Default Parameter Values in Signature Help #52819
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
Conversation
Given default values don't appear in d.ts files, this doesn't seem like a good idea in general because what the tooltips show will end up differing depending on how the project is loaded, right? |
I don't really get what you mean, so you think it should be extended with support for looking into source definition of function and trying to figure out the default value for parameter? Sounds interesting for some libs (but not many I think). Also, I feel request is coming from working with large codebases first, not for libs that mostly well-documentated. But again, even in d.ts there is a way to specify default values for parameters: interface A {
/**
* @param {} [youMakeMeHappy=true] test
*/
(youMakeMeHappy: boolean)
} It's just a matter of d.ts generation process |
The case I'm thinking of is a composite project (or maybe just references + declaration?); my understanding (someone should correct me if I'm wrong) is that if a dependency already has |
Yes, I understand your use case of using d.ts, I was using it myself in monorepo setup. Extending my comment above, Maybe we can change the process of generation d.ts later to ensure default values aren't lost? |
How would you emit a d.ts file from code like this? function foo(x = someComputedValue()) { ... } We'd have to start limiting this to just specific kinds of literals that we can rewrite (as the above likely wouldn't be workable), then track down all of the existing code which assumes that d.ts files won't ever contain anything in value space. I'm skeptical it's worth it, but, Python type stubs recently changed to start allowing default values, so... 😅 |
Hm.. It seems I might be missing something, why not something like this?: /**
* @param {} [x=someComputedValue()]
*/
declare function foo(x?: any): void; The same content would be displayed in signature help. I don't see anything bad here. I think this is behavior that everyone expects: just copy the initialzer text. So how can it be improved in theory? |
and rename prev test
The TypeScript team hasn't accepted the linked issue #16665. If you can get it accepted, this PR will have a better chance of being reviewed. |
@jakebailey I think I now understand what did you mean in #52819 (comment). Since I made changes to general method of typechecker Also notice that this pr changes typescript lib declarations (I'm now adding |
Okay, I think I've completed it, so will be waiting for your review. Let me know if you have any thoughts |
@zardoy Can you explain your implementation choices back on the original bug, as well as describing how the PR addresses the questions raised there? That would help get it accepted; I don't want to put too much effort into reviewing non-accepted bugs, and it looks like the barrier for this one was an uncertainty around some semantics, plus deciding whether it's OK to output the entire type when it's large. |
Thank you so much for viewing this one! Of course, I don't mind having a discussion before a review. First of all, I want to make the following clear: Public API changes:
I think either me or you might be missing something here, as initializers aren't types, and doesn't signature help already output types even when they are large? What considerations can be here? eg: const a = (a: (a: (a: (a: (a: () => void) => void) => void) => void) => void) => {}
a(/* parameter type in signature help takes more than one line */) I'd like to answer more questions here, but can point me to specific things that were unclean? Thanks! |
This was the semantic question I was thinking of:
What this means is: this feature will work for .ts files in your own project, but will not work with .d.ts files -- and the latter would be the ones that get the most benefit from it. I think @jakebailey 's concern is that, in addition, it's inconsistent, so people won't be able to guess. @jakebailey can you tell me if I understood your earlier comment? Regarding size of signature help, the reason I'm concerned is: if some percentage of signature help results are already too big, doubling the size of signature help will make a higher percentage of results too big. Also, I believe types have truncation past a certain size (although the limit is still too high in my opinion). Does this PR truncate initialiser values? How are values truncated? |
Yes, that's my impression. That and this seems to pull info from JSDoc in TS files, which we generally do not do (see signatureHelpParameterDefaultDeclaration.ts). Truncation is definitely another problem. As far as I'm aware, truncation works by adding up a total length as we go from left to right, and at some point we quit, meaning that the more text we add, the less we show to the right. |
As far as I understand, the only truncation we have here is |
During |
I see, you mean |
Truncation happens for diagnostics as well as for editors like tools e.g. signature help, so it definitely matters here. |
@zardoy this has 3 open questions, one about usability from .d.ts files, one about use of JSDoc in TS files and one about tradeoffs in how much gets displayed. My guess is that the first problem will not be easily solvable. Do you want to keep working on this? |
Thank you so much for coming back to this one!
I see a pretty big interest in this feature, so I would be happy to talk about these open questions
obviously we can't do anything with existing libraries and other existing .d.ts, we just simply don't have this information, but in future, we can change the process of generating .d.ts files to include such information, it's already known that adding
As I understand this one is related to the previous one
any ideas? I think the most common case is a simple literal value such as
To be honest, I don't have much time now, but I would be happy to continue negotiating the current code changes & the general idea and maybe even continue developing it. |
Unfortunately, we haven't had time to work out the design of the feature, and this PR is quite old now. I think it makes most sense to close this PR and re-open it once the open questions have been resolved in discussion on the original issue. |
Fixes #16665
Handled cases:
const a = (b = true) => {}
.It was suspicious easy to fix for regular parameter initialzer. In some cases (I think it should be really rare) initialzer can be really long. So answering @a-tarasyuk #16665 (comment) :
Will be shown as
Is that okay? 🤷
I don't like the idea of hiding information in this case, but trying to merge it to format like
{ c = 2 as number }
would be cool imoI changed parser.ts in order to store jsdoc optional default parsed expression in node. (seems it broke everything)
Will add tests tomorrow.