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.

Let jsxImportSource be configurable through tsconfig.json

See original GitHub issue

Is your proposal related to a problem?

Right now it’s only suggested for tsconfig.json to set "jsx": "react" but it’s not enforced.

TS 4.1 will bring new JSX-related option (jsxImportSource) and new possible values for the jsx option (react-jsx & react-jsxdev) - both of those changes are related to the new JSX runtimes.

TS also has an ability to change what exact JSX namespace is used for type-checking based on the used jsx factory as per the documentation. This probably means that there is a problem for some possible configuration today: TS can do its typechecking thinking that another factory is used (when one would configure jsxFactory) but in fact the emitted code uses the default factory of React.createElement because the transpilation is done by Babel that is not aware of TS configuration.

I’m not really interested in fixing this problem - I haven’t found any issue about this so the audience for this is probably very little and it’s just a possible problem that I have thought of and not something that affects me.

However, I would really love to be able to configure jsxImportSource globally and while you don’t allow for Babel customization (which is understandable tradeoff here) you allow for some tsconfig.json configuration. It (tsconfig.json) seems to be a good match for inferring this particular configuration for a vast number of consumers (as TS is used by a lot of people).

Motivation

I’m a maintainer of Emotion which is a popular CSS-in-JS solution for React (3.4M downloads/week) which comes with the CSS prop API that is based on @jsx pragma (although not all of our consumers use the CSS prop API as we expose the popular styled API as well).

Right now the @jsx pragma in CRA has to be used all over the place. We personally don’t find it to be that problematic but we get complaints about this regularly from the community. Having a way to configure this (new transforms, not the old pragma) globally in CRA would probably result in a huge DX improvement for us.

Describe the solution you’d like

Provide custom importSource to @babel/preset-react, based on the jsxImportSource from tsconfig.json

Describe alternatives you’ve considered

If this is something that you don’t want to support then I strongly believe that you should disallow configuring jsxImportSource in the tsconfig.json altogether because it causes a configuration mismatch between TS and Babel and this might be a footgun for people.


  • Implementation intent

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:58
  • Comments:22 (5 by maintainers)

github_iconTop GitHub Comments

17reactions
donaldkgcommented, Dec 10, 2021

This issue is confusing and some documents say this typescript config works but actually it is not. Do you have any updates on this issue?

12reactions
MatthieuCoelhocommented, Dec 3, 2020

With version Typescript 4.1 I tried

"jsxImportSource": "@emotion/react",
"jsx": "react-jsx"

But the css props from emotion is not working, I have to put “/** @jsxImportSource @emotion/react */” on top on the file.

Is the property jsxImportSource ignored ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

TSConfig Option: jsxImportSource - TypeScript
TSConfig. jsxImportSource. Declares the module specifier to be used for importing the jsx and jsxs factory functions when using jsx as "react-jsx" or ......
Read more >
Cannot use JSX unless the '--jsx' flag is provided
Every time I run npm start , it overrides whatever I configure in {jsx: ... The following changes are being made to your...
Read more >
TypeScript - Parcel
In addition to stripping the types to convert TypeScript to JavaScript, ... A tsconfig.json file can be used to configure some aspects of...
Read more >
Understanding TypeScript Configuration Options
This post will walk you through how tsconfig.json works in your application ... When allowJs is true , TypeScript will allow JavaScript files...
Read more >
TypeScript | Preact: Fast 3kb React alternative with the same ...
Add the following configuration to your tsconfig.json to transpile JSX to Preact-compatible JavaScript: // TypeScript < 4.1.1 { "compilerOptions": { "jsx": ...
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 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