-
Notifications
You must be signed in to change notification settings - Fork 30.2k
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
Support .tsx
file extensions.
#56322
Comments
Would you like to send a PR? |
Before working on it, I would like to discuss and reach consensus that the Node.js maintainers would like to include this feature. |
I'm not sure what there is to discuss, but in any case, it won't happen until someone submits a PR. //cc @nodejs/typescript |
I see, so from my perspective here is what I think we need consensus on:
imho this would solve the outlined problem for most users, but some might have a desire to support JSX directly in the future in some form. |
Many of the arguments cited in favour seem illogical to me. It seems like you're suggesting supporting a mismatched file extension and then erroring when the file extension is used correctly? Supporting actual TSX would be quite a hairy ordeal, and encouraging people to misuse the Or have I misunderstood you? |
Yes, you misunderstood. Using the |
It seems I did not. That is a misuse of the file extension, and it would be a landmine for users. We should absolutely not do that. However, if we were to support jsx and thus tsx, that would be a different story. One would need to figure out how to handle configuration, because jsx has multiple transpile options. I could see value in that, but I would advise first sketching out some broad-strokes before jumping to a PR (which would almost certainly result in contradicting opinions). Far better to get those to consensus before churning on an implementation. Happy to answer questions for that 🙂 I don't know the appetite of others (I'm not sure of my own opinion on it) to support jsx though. |
How is it not a misuse of a file extension to do that? That's what |
There are many projects with files that use the On the topic of partial support, Node.js implemented |
It seems to me that supporting files with a
This proposal is more like saying “why not support the |
I explored this idea but I'm against it. |
Would you mind elaborating on this sentence? |
If types are removed, all its left is jsx that cannot be run without an external loader or a framework (that uses a loader) |
The logical approach, considering the previously discussed While I agree with adding support for Introducing a new flag might seem exhaustive, but it aligns with the established decision-making process. Moreover, the JSX implementation should remain independent of TypeScript-specific features, much like how This involves two distinct steps:
Providing only one of these features without the other is both counterintuitive and likely to confuse users. |
A potential first step toward this approach could be to simply ignore If this change is released with a clear note explaining that it currently ignores This would provide a practical and usable solution much earlier, giving developers a path forward without having to wait for the complete implementation of a |
I'd first try to get consensus for jsx support, if that happens adding type stripping to tsx is easy. |
The opposite would allow jsx usage via custom loaders, waiting for jsx support means no jsx at all until that + it would bring more people to use |
JSX has (at least) two issues that would need addressing:
JSX isn’t only used by React. For example, this JSX: export default function Test() {
return (
<h1>Hello World!</h1>
)
} compiles per this site into this JavaScript for React: export default function Test() {
return /*#__PURE__*/ React.createElement("h1", null, "Hello World!");
} and this for Vue: import { createVNode as _createVNode, createTextVNode as _createTextVNode } from "vue";
export default function Test() {
return _createVNode("h1", null, [_createTextVNode("Hello World!")]);
} |
JSX indeed has specific targets, primarily <h1>Hello world</h1>
import { jsx as _jsx } from "react/jsx-runtime";
_jsx("h1", { children: "Hello world" });
import React from "react";
React.createElement("h1", null, "Hello world");
The combination of them is what is done for any library that supports JSX, such as Vue.js, Kita, React, Preact, or Astro. |
It seems great to support jsx syntax somehow, but the closest "default" transformation of jsx is for react, and i'm not sure that's what everyone would want. As for
I think this is just an incorrect "want" - it's not like their images and JSON files are going to use the same extension, because the point of a file extension is 0% aesthetics and 100% accurately conveying the parse goal of the file. |
This exists. This discussion is going in circles. You are not addressing the problems this proposal creates, despite us explaining them in multiple different ways. TS cavorting as TSX is a hard "no" by many. In the interest of sparing valuable time: I will block this (as others have indicated they will too). JSX support is a maybe, dependent on design. I am happy to help you outline a possible plan forward, although I do not necessarily support it—I share Marco's concerns: the current implementation is brilliant in its simplicity, and this seems contrary and counter to that. If you wish to pursue supporting JSX, please open a new issue. If you would like my help with that, please tag me. |
what is the workaround for this right now ? |
Please see the comment immediately above 🙂 |
|
I'm the author of |
What is the problem this feature will solve?
Node's
--experimental-strip-types
and--experimental-transform-types
are great improvements that allow us to drop a lot of tooling to run TypeScript in node. Right now, it only supports the.ts
extension and does not work on.tsx
..tsx
only really exists because of syntax ambiguity with a (somewhat legacy) way of typecasting in.ts
files: microsoft/TypeScript#26489 (comment)Why would you use
.tsx
for backend code?In a monorepo, you might have many packages shared by the client and backend. For consistency, those projects may want to use only a single file extension like
.tsx
. If you use React, which many people do, and some files use.ts
and some.tsx
, it's always jarring to type JSX in a.ts
file, realize it's a syntax error, and then having to rename the file. The renames also make it harder to navigate git history. Finally, if you are using only a single extension, TypeScript's behavior is always the same with regards to how angle brackets work.I'd be excited to implement this feature and send a Pull Request if the Node.js team is in favor of supporting
.tsx
extensions.What is the feature you are proposing to solve the problem?
I imagine the most workable solution is to support
.tsx
extensions just like.ts
, but throw an error if JSX is actually used. I'm not advocating for supporting JSX, just for node to support.tsx
files without compiling JSX.What alternatives have you considered?
The alternatives are to keep using third-party tooling to strip TypeScript types, or to rename all backend files using
.tsx
to.ts
, which may not be possible in large monorepos given the amount of files affected, and as mentioned previously, makes git history more cumbersome to work through.The text was updated successfully, but these errors were encountered: