tsx-to-ts
v2.0.0
Published
A tool to convert TSX files to TS files
Readme
tsx-to-ts
A command-line tool to convert TypeScript React (TSX) files to plain TypeScript
(TS) files by transforming JSX syntax into React.createElement calls.
Installation
npm install tsx-to-tsUsage
npx tsx-to-ts "src/**/*.tsx"This will:
- Find all TSX files matching the glob pattern
- Convert JSX syntax to React's automatic JSX runtime (
jsx/jsxsfunctions) - Save new
.tsfiles alongside the original.tsxfiles
How it Works
The tool uses:
@babel/corefor code transformation@babel/plugin-transform-react-jsxwith automatic runtime to convert JSX syntax- Babel's TypeScript parser for parsing TSX files
For example, this TSX:
const Button = () => <button type="submit">Click me</button>;Gets converted to:
import { jsx as _jsx } from "react/jsx-runtime";
const Button = () =>
_jsx("button", {
type: "submit",
children: "Click me",
});The tool uses React's automatic JSX runtime, which transforms JSX elements into
jsx and jsxs function calls from react/jsx-runtime.
Development
Clone the repository
Install dependencies:
pnpm install- Build the project:
pnpm build