Support JSX shorthand properties, for compatibility with Bun
See original GitHub issueSuggestion
š Search Terms
- jsx
- tsx
- shorthand
- properties
ā Viability Checklist
My suggestion meets these guidelines:
- This wouldnāt be a breaking change in existing TypeScript/JavaScript code
- This wouldnāt change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isnāt a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScriptās Design Goals.
ā Suggestion
Bun implements JSX shorthand properties, presumably because they are extremely convenient to use, even though they havenāt been added to the JSX spec, presumably because thatās effectively dead.
š Motivating Example
Example code:
const onClick = console.log;
const vdom = <div {onClick} />;
console.log ( vdom );
Basically {foo}
is just a shorthand for foo={foo}
. The same way outside of JSX {foo}
is a shorthand for {foo: foo}
.
š» Use Cases
Itās a very natural extension of the syntax, and extremely convenient too, for many components that I personally write about half of the JSX is just redundant and could be deleted if I could use shorthand properties.
As massive of an improvement it is though I canāt give up type-checking for it.
Basically the engine Iād like to use supports it already, the code I tend to write screams for this feature, but the JSX spec is abandonware and TS doesnāt support it.
Can we please consider adding it and improve the lives of millions of developers? If thatās not a strong argument enough can we consider adding this for compatibility with Bun?
Iād be more than happy to submit a PR for this.
//cc @Jarred-Sumner
Issue Analytics
- State:
- Created 9 months ago
- Reactions:135
- Comments:17 (5 by maintainers)
Top GitHub Comments
As a workaround you can use object spreading:
Works well with multiple attributes as well:
No idea why some people have the urge to downvote this comment when all I do is provide a viable option that already works and is supported by both JSX and TypeScript.
Iām going to push back against both of these statements. I donāt find it natural, and I donāt see it as an improvement. I find the syntax noisy and annoying to scan, because now I have to add one more syntax in my head to keep track of when it comes to passing props. It might make writing props marginally easier by saving a few keystrokes, but it will make reading those props a bit more aggravating, especially in scenarios where both the shorthand and longhand are used.
The more ways you have to achieve a single task, the more complex the language becomes, and the harder it is to learn and teach. Even for somebody like me whoās been writing JS professionally for 25 years, and TS since it was introduced, Iād much prefer the language move in the direction of being easier to read and understand, even if it means I have to type a few more characters.