@canonical/typescript-config

中文文档
0.44.0发布于 2026/10/23LGPL-3.0零依赖构建来源已验证近 30 天下载 9471

Canonical's standard TypeScript configuration

原始使用文档
文档目录4 节
  1. Getting Started
  2. Configuration
  3. History
  4. Caveats

Canonical Typescript Configuration

This package provides a central configuration for Canonical's TypeScript projects.

For React projects, see the React configuration.

Getting Started

  1. Install Typescript: bun add -d typescript
  2. Install this configuration: bun add -d @canonical/typescript-config
  3. Create a tsconfig.json file in the root of your project and extend this configuration.
{
  "extends": "@canonical/typescript-config"
}

Configuration

This configuration enables the following behavior:

  1. ES2023 library inclusion: Includes the ES2023 library for TypeScript projects.
  2. NodeNext module: enables module resolution for Node projects with both CJS & ESM support.
    1. Explicit import file extensions (e.g., ./Button/index.js instead of ./Button).
  3. ES2023 target: Targets the ES2023 ECMAScript version. Emits modern JavaScript syntax, omits legacy transforms and polyfills.
  4. 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"] (DOM can 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/.

版本与发布记录

@canonical/typescript-config 共发布 34 个版本,首次发布于 2026/02/10;过去 12 个月发布了 34 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.44.0latest2026/10/2317 天前
0.43.02026/10/011 个月前
0.43.0-experimental.0experimental2026/09/291 个月前
0.42.02026/09/281 个月前
0.40.02026/09/211 个月前
0.38.02026/09/161 个月前

其他发布通道:a → 0.12.0,experimental → 0.43.0-experimental.0。

关于 @canonical/typescript-config 的常见问题

@canonical/typescript-config 是什么?

Canonical's standard TypeScript configuration

如何安装 @canonical/typescript-config?

在项目目录运行 npm install @canonical/typescript-config,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:typescript(^5.9.3 || ^6.0.0 || ^7.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@canonical/typescript-config 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/canonical__typescript-config 是否存在。

@canonical/typescript-config 可以商用吗?

@canonical/typescript-config 使用 LGPL-3.0 许可。弱 Copyleft:作为库被调用时通常不要求开源你自己的代码,但修改库本身需以 LGPL 开源;前端打包合并后的适用边界存在不同理解。以上为概括,不构成法律意见,请以许可原文为准。

@canonical/typescript-config 的下载量有多少?

根据 npm 官方下载统计,@canonical/typescript-config 在 2026/10/09 – 2026/11/07 这 30 天内被下载 9,471 次,日均约 316 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@canonical/typescript-config 还在维护吗?

最新版本 0.44.0 发布于 2026/10/23(17 天前);至今共发布 34 个版本,其中过去 12 个月发布了 34 个;首次发布于 2026/02/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。