question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

Support JSX shorthand properties, for compatibility with Bun

See original GitHub issue

Suggestion

šŸ” 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:open
  • Created 9 months ago
  • Reactions:135
  • Comments:17 (5 by maintainers)

github_iconTop GitHub Comments

35reactions
MartinJohnscommented, Dec 31, 2022

As a workaround you can use object spreading:

const onClick = console.log;
const vdom = <div {...{onClick}} />;

Works well with multiple attributes as well:

const onClick = console.log;
const onBlur = console.log;
const vdom = <div {...{onClick, onBlur}} />;

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.

24reactions
rossipediacommented, Dec 30, 2022

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.

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JSX In Depth
Fundamentally, JSX just provides syntactic sugar for the React. ... the ES6 object shorthand {foo} which is short for {foo: foo} rather than...
Read more >
styled-components: Basics
Use Title and Wrapper like any other React component ā€“ except they're styled! ... Note that the flex property works like CSS shorthand,...
Read more >
ECMAScript 6 compatibility table
Feature nameā–» Current browser 98% ES6 Transā€‘ piler 25% Trace... Optimisation Optimisation Optimisation Optimi... Ā§proper tail calls (tail call optimisation)ā–» 0/2 0/2 0/2 Ā§direct recursionc No...
Read more >
JSX or HTML autocompletion in Visual Studio Code
2022: Straightforward way to add JSX/HTML autocomplete for .js files in React ... Visual studio code detect .jsx extensions and add emmet support...
Read more >
JavaScript Environment
If Hermes is disabled, React Native will use JavaScriptCore, ... JavaScript syntax without having to wait for support on all interpreters.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found