single-spa TypeScript Configuration
ts-config-single-spa provides a foundational TypeScript configuration preset designed specifically for projects within the single-spa microfrontend ecosystem. Currently at version 3.0.0, this package aims to standardize `tsconfig.json` settings, ensuring compatibility and best practices across various microfrontends. It handles common configurations such as `target`, `module`, `jsx` support, and `moduleResolution`, which are critical for smooth integration in a polyglot microfrontend architecture. While there isn't a strict independent release cadence, it is part of the larger `create-single-spa` monorepo and updates are coordinated with other single-spa tooling, ensuring ongoing relevance and support. Its key differentiator is simplifying TypeScript setup for single-spa, minimizing boilerplate, and reducing common configuration pitfalls associated with module interoperation and shared dependencies in a microfrontend environment.
Common errors
-
TS2307: Cannot find module 'single-spa' or its corresponding type declarations.
cause TypeScript cannot locate the type definitions for the `single-spa` package, often due to incorrect `moduleResolution` or missing `@types/single-spa`.fixInstall the official type declarations: `npm install --save-dev @types/single-spa`. Ensure `compilerOptions.moduleResolution` is set to `"node"` (or `"bundler"` for newer TS versions) in your `tsconfig.json`. If using `type: "module"` in `package.json`, check for `package.json` `"exports"` configuration issues for `single-spa` itself. -
Error: TS6053: File '...' not found. OR TS1005: ',' expected.
cause Your `tsconfig.json`'s `include` or `exclude` paths are incorrect, or the `extends` path for `ts-config-single-spa` is wrong.fixVerify the `extends` path: ensure `"ts-config-single-spa"` is correctly specified. Check `include` and `exclude` arrays for typos or incorrect glob patterns. Ensure the `ts-config-single-spa` package is correctly installed in `node_modules`. -
Property 'singleSpa' does not exist on type 'Window'.
cause The global `window.singleSpa` object's type is not recognized by TypeScript, often because `single-spa` types are not properly loaded.fixEnsure `@types/single-spa` is installed. If manually declaring types, create a `global.d.ts` file with `declare global { interface Window { singleSpa: any; // Or a more specific SingleSpaGlobal type } }` to extend the global `Window` interface.
Warnings
- breaking Upgrades to newer TypeScript versions (e.g., TypeScript 6.0+) can introduce breaking changes in default compiler options (`strict`, `module`, `target`, `moduleResolution`, `rootDir`). `ts-config-single-spa` will eventually adopt these, which may require explicit configuration in your local `tsconfig.json` to maintain previous behavior if not using strict defaults.
- gotcha Incorrect module resolution (`moduleResolution`) or `paths` configuration can lead to 'Cannot find module' errors, especially in monorepos or when importing shared utility modules across single-spa applications. This is a common issue when `webpack-config-single-spa-ts` is misconfigured.
- gotcha When using `webpack-config-single-spa-ts` or similar configurations, ensure that your `webpack.config.js` correctly handles `externals` for shared dependencies (like React, Vue, single-spa itself) to prevent them from being bundled into each microfrontend. This config package doesn't directly manage `webpack` externals, but relies on the overall build setup.
- deprecated TypeScript 6.0 deprecates `target: "es5"` and `moduleResolution: "node"` (which refers to Node 10-style resolution). While `ts-config-single-spa` may support older targets, modern `single-spa` setups increasingly leverage newer ECMAScript features and module systems like ESM and import maps.
- gotcha TypeScript's default `lib` option might not include `dom.iterable` or `dom.asynciterable` in older configurations, leading to type errors for modern browser APIs. TypeScript 6.0 introduces `dom.iterable` and `dom.asynciterable` into the default `dom` lib.
Install
-
npm install ts-config-single-spa -
yarn add ts-config-single-spa -
pnpm add ts-config-single-spa
Imports
- TsConfigExtends
import 'ts-config-single-spa';
{ "extends": "ts-config-single-spa" } - CompilerOptions
import { CompilerOptions } from 'ts-config-single-spa';// Your tsconfig.json merges with or overrides options defined by ts-config-single-spa // Example: { "compilerOptions": { "jsx": "react-jsx" } } - GlobalTypes
import { SingleSpaGlobal } from 'ts-config-single-spa';/// <reference types="single-spa" /> // or install @types/single-spa
Quickstart
{
"extends": "ts-config-single-spa",
"compilerOptions": {
"jsx": "react-jsx", // Adjust based on your framework (e.g., "preserve", "react", "vue-jsx")
"lib": ["dom", "es2020"],
"baseUrl": ".",
"paths": {
"*": ["src/*"]
},
"declaration": true, // Often desired for microfrontends exporting modules
"outDir": "dist"
},
"include": ["src/**/*.ts", "src/**/*.tsx"],
"exclude": ["node_modules", "dist"]
}