When creating a TypeScript React app, I used the following command:
yarn create react-app my-app --template typescript
This setup compiles my project using Babel and bundles it with webpack. Now, I want to utilize TypeScript namespaces, which are not natively supported in Babel. However, they can be enabled by installing certain packages:
I made adjustments in the package.json
file, changing react-scripts start
to react-app-rewired start
.
Additionally, I created a custom configuration file named config-overrides.js
:
const {
override,
addExternalBabelPlugin
} = require("customize-cra");
module.exports = override(
addExternalBabelPlugin([
"@babel/plugin-transform-typescript",
{ allowNamespaces: true }
])
);
Despite these steps, compiling the project still results in a syntax error, indicating that the plugin for non-declarative namespaces was not properly enabled:
SyntaxError: /home/m93a/my-app/script.ts: Namespace not marked type-only declare. Non-declarative namespaces are only supported experimentally in Babel. To enable and review caveats see: https://babeljs.io/docs/en/babel-plugin-transform-typescript
What is the correct setup process to ensure that even non-declarative namespaces compile successfully in the project?
EDIT: The root cause of the issue with my project turned out to be different than expected. Refer to my own answer below for more insights.