-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Improve Function Signature Syntax #29019
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
Why not this: interface FooArgs {
arg_1?: number;
arg_2?: number;
arg_3?: number;
}
function foo(arg: FooArgs) {
const { arg_1 = 0, arg_2 = 0, arg_3 = 0 } = arg;
} ? |
Plus, this syntax { arg_1: number = 0 } means that you rename |
interface FooArgs {
arg_1?: number;
arg_2?: number;
arg_3?: number;
}
function foo(arg: FooArgs) {
const { arg_1 = 0, arg_2 = 0, arg_3 = 0 } = arg;
} has the same issue as mentioned in the OP. function foo({arg_1:number=0, arg_2:number=0, arg_3:number=0}){
} which eliminates code duplication. I'm not sure that the assignment syntax directly conflicts with the function signature syntax. Even if it does, the syntax I proposed is definitely open to adjustment. The main point is to reduce excess/duplicate code. This could just as easily be accomplished with foo({{arg_1: number = 0}})
foo({<number> arg_1 = 0}) or some other similar, non-conflicting syntax. |
As @dmytro-lymarenko said: the proposed syntax collides with javascript rename on destructuring arguments (using this example: This proposed function foo({arg_1:number=0, arg_2:number=0, arg_3:number=0}){
} has the problem that all there values must be stored to the same variable as you can see into playground. |
I addressed that issue in the second half of my response to @dmytro-lymarenko. The *exact* syntax that I proposed is not important, it can definitely be altered as needed to make it work. |
Still a dupe of #7576 and it's still declined for all the same reasons. |
I'd argue that it should be reconsidered/reopened, especially when considering the related issue in #5326. If these issues are not handled properly, then TypeScript's signatures will become even more redundant and troublesome to maintain than they already are. Also, this is clearly something that users want, as noted by the continued activity in #7576 after it was closed. Multiple users have even commented that they avoid using destructuring and/or TypeScript altogether because of these issues. The initial specification was designed poorly, and it should be rectified rather than implementing clumsy workarounds every time a new feature needs to be added. Breaking changes have been made for certain features, and I think that solving the cluttered function signature issues that currently exist would be worthy of a breaking change. Having a single-section function signature would then allow all future changes to be implemented much more easily. |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Suggestion
Please reconsider combining default parameters & type annotations in function signatures. This was already proposed in #7576, but the issue was closed shortly after @mhegazy's post which made the following argument:
However, in reality the current syntax is more difficult to write & maintain than the proposed syntax. I'd argue that the proposed syntax would even be worth admitting into Breaking Changes in the long run, given that the change is timed & documented appropriately (even if that means waiting for a major version release).
Examples
Note
A few people have mentioned that there may be a conflict with the
{a:b=c}
syntax. However, the exact syntax is not the purpose of this recommendation, and I invite any suggestions for a more preferable syntax that does not have any compatibility issues. The important part of this suggestion is that the syntax should be able to handle type annotations & default parameters in one section instead of two.Search Terms
destructuring destructure function signature default parameters type annotations
The text was updated successfully, but these errors were encountered: