React Static TypeScript Plugin
The `react-static-plugin-typescript` package integrates TypeScript support into projects built with the React Static framework, enabling the use of `.ts` and `.tsx` files for components and application logic. The current stable version is 7.6.2. It operates by hooking into React Static's build pipeline to compile TypeScript code and offers an optional type-checking feature during compilation. This plugin is a core component for React Static users who prefer TypeScript for improved code maintainability and developer experience. Its release cadence is tightly coupled with the React Static framework's updates. Key differentiators include its seamless integration into the `static.config.js` file and its ability to toggle type-checking during development builds, providing flexibility for different workflow needs. The React Static ecosystem appears to be in a maintenance state, meaning this plugin follows a similar lifecycle.
Common errors
-
Module not found: Error: Can't resolve 'typescript' in '...'
cause The `typescript` peer dependency is not installed in the project.fixRun `npm install typescript --save-dev` to install the TypeScript compiler. -
ERROR in TS[...] Property '...' does not exist on type '...'.
cause A TypeScript type error was detected during compilation when `typeCheck` is enabled. This prevents the build from completing.fixCorrect the TypeScript type errors in your source code. If the error is intentional or a temporary workaround, consider casting or modifying the type definitions. For faster local iteration, you may set `typeCheck: false` in the plugin options (not recommended for production builds). -
Error: No default export for template [...]
cause A React Static template (e.g., a page component or `src/App.tsx`) is missing a default export, which is required by the framework.fixEnsure all React Static page components and `src/App.tsx` have a `export default MyComponent;` statement. -
Failed to load tsconfig.json: [...]
cause The `tsconfig.json` file is either missing from the project root or contains invalid JSON syntax, preventing the TypeScript compiler from configuring correctly.fixVerify that `tsconfig.json` exists in your project's root directory and that its content is valid JSON. Use a JSON linter or editor to check for syntax errors.
Warnings
- breaking Node.js v10 support was removed across the React Static v7.2.0 ecosystem, including this plugin. Projects now require Node.js v12 or higher for compatibility.
- breaking The `v7.6.0` release of React Static brought an update to its `emotion` plugin to support Emotion v11. If your project uses Emotion, this might introduce breaking changes requiring updates to your Emotion configuration and component styling.
- gotcha Disabling type checking via the `typeCheck: false` option can lead to uncaught TypeScript errors during compilation, potentially resulting in runtime issues that would otherwise be caught at build time.
- gotcha Missing or incorrectly configured `tsconfig.json` will prevent the plugin from properly compiling TypeScript files, leading to build failures or unexpected behavior. Ensure it aligns with React and modern JavaScript practices.
- gotcha The `typescript` peer dependency must be installed in your project, along with `@types/react` as a dev dependency. Failure to do so will result in compilation errors or missing type definitions.
Install
-
npm install react-static-plugin-typescript -
yarn add react-static-plugin-typescript -
pnpm add react-static-plugin-typescript
Imports
- PluginConfiguration
import { ReactStaticTypeScriptPlugin } from 'react-static-plugin-typescript';export default { plugins: ['react-static-plugin-typescript'], } - PluginWithOptions
const pluginConfig = require('react-static-plugin-typescript'); // Incorrect for static.config.js and for passing optionsexport default { plugins: [['react-static-plugin-typescript', { typeCheck: true }]], }
Quickstart
npm install react-static-plugin-typescript @types/react --save-dev
// static.config.js (in your project root)
export default {
plugins: [
// Add the TypeScript plugin, optionally with typeCheck enabled
['react-static-plugin-typescript', { typeCheck: true }]
],
// Other React Static configurations...
}
// tsconfig.json (in your project root)
{
"compilerOptions": {
"target": "es2015",
"module": "esnext",
"lib": ["es2015", "dom"],
"moduleResolution": "node",
"skipLibCheck": true,
"isolatedModules": true,
"noEmit": true,
"allowJs": true,
"jsx": "preserve",
"sourceMap": true,
"removeComments": false,
"strict": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src"],
"exclude": ["node_modules", "dist", "build"]
}