Arrow function can be non intuitive for JS developers
See original GitHub issueCurrently y := x => x + 1
compiles to const y = x(() => x + 1)
which may be a source of confusion for people coming from js which allows parenthesis to be omitted for single argument.
Should we consider adopting the js behavior by default (unless coffeeCompat is set to true) ?
Perhaps to reduce ambiguity we enforce that arg list can never be omitted, so instead of y := -> 1
we’d have to write y := () -> 1
Issue Analytics
- State:
- Created 9 months ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
Arrow Functions vs Traditional Functions in JavaScript - Medium
Arrow functions give you more flexibility to move from minimal to verbose where traditional JavaScript functions only have one form: verbose. Arrow functions ......
Read more >When Not to Use Javascript Arrow Functions - Live Code Stream
An arrow function expression is a syntactically compact alternative to a regular function expression, although without its own bindings to the ...
Read more >Arrow functions vs Regular functions in JavaScript
Arrow functions allow a developer to accomplish the same result with fewer ... Another place arrow functions make for cleaner and more intuitive...
Read more >JavaScript Arrow Functions: How, Why, When (and ... - ZenDev
Arrow functions have a single overarching structure, and then an number of ways they can be simplified in special cases. ... A list...
Read more >Understanding Arrow Functions in JavaScript | DigitalOcean
Arrow functions are a new way to write anonymous function expressions in JavaScript, and are similar to lambda functions in some other ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@lorefnon TypeScript doesn’t allow Type annotations on paren-less single parameters (another reason I choose to disallow them). There is definitely room to improve error messages in the LSP and I’d be happy for you to take a look.
The recommended Civet way to type annotate your example is:
Note the annotation is for the type of
z
to the left of the const assignment:=
.In cases where the ampersand function is used inline TS can usually infer the type correctly.
@edemaine in light of these examples I think that allowing inline type annotation of ampersand functions makes the intent a bit less clear but I’d be happy to look at some counter examples.
Also, I want to say thank you both for your contributions! It really helps me work out the practical details of the language to see how people feel most comfortable using it.
Would
&: number + 1
or(&: number) + 1
be interesting typed shorthand for($: number) => $ + 1
?