-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Add mechanism for specifying the type of a function declaration #37433
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
Comments
This reminds me of this suggestion: #34319 |
Duplicate #22063 |
Thanks for the links @ilogico and @RyanCavanaugh, it seems my searchbar-fu was not strong enough today. It looks like they both describe the same root problem, though with slightly different implementation suggestions. I'd be for rallying around a single issue, and dumping all possible syntaxes into one thread. @RyanCavanaugh Any preference on which of those two issues we should keep open? |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Did this functionality ever get added to typescript? I couldn't tell if there was conclusion that was reached on this topic. I like the following syntax:
|
@offg777 It wasn't. I suppose you can vote on the linked issues. |
How do I do that? |
Search terms
function definition type, function expression
Suggestion
I think it would be useful to add a mechanism for specifying the type of a function definition, i.e a type that defines arguments and a return type like
type Stringifyer = (arg: number) => string;
. This is currently possible by using function expressions but is not possible with function declarations, this feels like a gap that leads to a slightly sub optimal developer experience, as using a function expression results in having to store two types and causes the developer to have to repeat their function names.Use Cases
Consider a case where I want export a function that should adhere to a predefined interface. In these examples I'll use a trival
Stringifyer
type as an example but a more concrete use-case would be writing a React component function and wanting to type your function as aReact.FunctionComponent
.This is possible using a function expression as follows:
Both of these examples have their faults.
Examples
A few ideas for what this could look like:
I'm thinking keeping the type close to the name of the function is useful
Out of these three I'm leaning towards option 3 if there are no parser issues. I don't thiiink there will be as the
function
keyword continues to come first so I guess the parser would know it' is in a function declarationChecklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: