-
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
typeof should be able to apply directly to function calls #54999
Comments
|
There is a fundamental difference between this and my suggestion. One is to simply obtain the return value type of the function, and the other includes a complete type inference process for the function. I have raised a question about the specific situation in stack overflow:https://stackoverflow.com/questions/76668843/why-cant-you-get-the-return-type-of-a-function-call-directly-through-typeof-in |
The types must be known at compile time, so it doesn't matter if you provide the type as an argument, or a type argument. So you can just do: declare const fn: <T>(arg: T) => T extends string ? number : boolean;
type a1 = ReturnType<typeof fn<"abc">>; // number
type a2 = ReturnType<typeof fn<123>>; // boolean |
'fn(123)' is to test the whole type inference process of the function fn, and 'fn<123>' is just to get a specific type, I don't know if I can express myself by saying that? |
I’m interpreting the ask here as “if I can write So this is a duplicate of #6606. That issue is closed as too complex, and there are other open issues for parts of it unsatisfied by conditional types (such as #40179 for function calls specifically) but nothing for the whole of #6606. I’m not suggesting that this issue be closed or that it stay open; just providing info on related issues to (hopefully) help with triage. |
Yes, this expression is very concise and effective. I think the current typeof is incomplete, because the proposed usage is very consistent with most people's intuition and long-term programming habits. This feature has sufficient weight, so I hope to strive for official attention to this function. This is not a feature that should be rejected on the grounds that it is too difficult to implement. |
Regardless, this #6606 (comment) feels like a pretty big hurdle to overcome (note: Ryan is the TS project lead). Apparently the thinking isn't so much "this would be too difficult to implement" but rather "this would make the language itself too complicated". If you ultimately only care about function calls, #40179 is still open and |
This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Suggestion
🔍 Search Terms
typeof function return
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
The following operations should be supported
The following is written in the manner now supported
It is obvious that the style of writing now supported is redundant,if I use analogy, I think it is like a programming language declaration and initialization. If a programming language supports declaration and initialization separately, then the declaration and initialization written together should also be supported.
The text was updated successfully, but these errors were encountered: