Following up the discussion in the comments, I'm sharing a config with the same source code as @caTS that does not reproduce the problem, but with rollup as a bundler.
This set up does not simply turn off type safety, I get compile errors from @rollup/plugin-typescript
when applicable.
The folder structure is the following:
public/
- dist/
- index.html
src/
- App.tsx
- index.tsx
- types.ts
- package.json
- tsconfig.json
- .babelrc
- rollup.config.js
package.json
{
"type": "module",
"scripts": {
"server": "http-server",
"build/watch": "rollup --config --watch"
},
"dependencies": {
"react": "18.0.0",
"react-dom": "18.0.0"
},
"devDependencies": {
"@babel/core": "^7.18.13",
"@babel/preset-react": "^7.18.6",
"@rollup/plugin-babel": "^5.3.1",
"@rollup/plugin-commonjs": "^22.0.2",
"@rollup/plugin-node-resolve": "^13.3.0",
"@rollup/plugin-replace": "^5.0.0",
"@rollup/plugin-typescript": "^9.0.1",
"@types/react": "^18.0.21",
"@types/react-dom": "^18.0.6",
"http-server": "^14.1.1",
"tslib": "^2.4.0",
"typescript": "^4.8.3"
}
}
tsconfig.json
{
"compilerOptions": {
"rootDir": "./src",
"moduleResolution": "node",
"module": "ES2020",
"target": "ES2020",
"strictFunctionTypes": true,
"noImplicitAny": true,
"strict": true,
"jsx": "react-jsx"
},
}
.babelrc
{
"presets": ["@babel/preset-react"]
}
rollup.config.js
import { nodeResolve } from '@rollup/plugin-node-resolve';
import { babel } from '@rollup/plugin-babel';
import commonjs from '@rollup/plugin-commonjs';
import typescript from '@rollup/plugin-typescript';
import replace from '@rollup/plugin-replace';
const environment = 'development';
const plugins = [
replace({
'process.env.NODE_ENV': JSON.stringify(environment),
preventAssignment: true
}),
typescript({
target: 'es5',
module: 'ESNext',
}),
babel({
babelHelpers: 'bundled',
exclude: [/\/node_modules\//]
}),
commonjs(),
nodeResolve(),
];
export default [{
input: './src/index.tsx',
output: {
file: './public/dist/index.js',
sourcemap: true,
format: 'es'
},
plugins
}];
src/App.tsx
import type { SomeType } from "./types";
const SomeType = ({ s }: { s: SomeType }): JSX.Element => {
return <>${s}</>;
};
export default function App() {
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
</div>
);
}
src/index.tsx
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import App from "./App";
const rootElement = document.getElementById("root");
const root = createRoot(rootElement!);
root.render(
<StrictMode>
<App />
</StrictMode>
);
src/types.ts
export type SomeType = JSX.Element
public/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<div id="root"></div>
<script type="text/javascript" src="./dist/index.js"></script>
</body>
</html>