@canonical/typescript-config-base
v0.10.0-experimental.4
Published
Canonical's standard TypeScript configuration
Maintainers
Keywords
Readme
Canonical Typescript Configuration
This package provides a central configuration for Canonical's TypeScript projects.
For React projects, see the React configuration.
Getting Started
- Install Typescript:
bun add -d typescript - Install this configuration:
bun add -d @canonical/typescript-config-base - Create a
tsconfig.jsonfile in the root of your project and extend this configuration.
{
"extends": "@canonical/typescript-config-base"
}Configuration
This configuration enables the following behavior:
ES2023library inclusion: Includes theES2023library for TypeScript projects.NodeNextmodule: enables module resolution for Node projects with both CJS & ESM support.- Explicit import file extensions (e.g.,
./Button/index.jsinstead of./Button).
- Explicit import file extensions (e.g.,
ES2023target: Targets theES2023ECMAScript version. Emits modern JavaScript syntax, omits legacy transforms and polyfills.- Strict mode: Enables strict type checking & several other strictness settings.
History
This configuration is rooted in the Canonical React Components Typescript config. It has been generalized to support non-React projects and be more minimal.
The following options have been changed from the original configuration:
"module": "esnext"→"module": "NodeNext": For stricter Node-compatible output and module resolution, greater import syntax consistency, and better ESM/CJS interoperability."target": "es6"→"target": "ES2023""lib": ["dom", "dom.iterable", "esnext"]→"lib": ["ES2023"](DOMcan be added manually if needed for browser projects)"strict": false→"strict": true"noUnusedParameters": true→ Subsumed by strict mode"noUnusedLocals": true→ Subsumed by strict mode"noImplicitReturns": true→ Subsumed by strict mode"noFallthroughCasesInSwitch": true→ Subsumed by strict mode"skipLibCheck": true→ Disabled for stricter dependency type checking"esModuleInterop": true→ Subsumed by"module": "NodeNext", which combines Node configuration with ESM interoperability"allowSyntheticDefaultImports": true→ Disabled for stricter import checking
Caveats
NodeNext vs. Node module resolution
When using TypeScript's NodeNext module resolution, TypeScript expects files to follow Node.js's ES Module (ESM) resolution rules, which require explicit .js extensions for imports. This approach matches ESM conventions, where the index.js file must be specified. This differs from Node module resolution, which defaults to CommonJS and allows directory imports without specifying an index file. Best Practices:
Use Explicit File Extensions: With NodeNext, include file extensions (e.g., ./Button/index.js).
Maintain an index.ts for Exports: Collect all exports in a single index.ts at the package root for easier imports.
Consider the Module Type: Use Node resolution if CommonJS is preferred; otherwise, NodeNext for ESM compliance.This approach maintains compatibility with both Node.js and bundlers.
In other words, by using NodeNext, we are aiming at compatibility with the ES Module specification, and this requires importing explicitly mymodule/index.js files instead of simply mymodule/
