tools.tsChecker

  • 类型: Object | Function
  • 默认值:
const defaultOptions = {
  typescript: {
    // set 'readonly' to avoid emitting tsbuildinfo,
    // as the generated tsbuildinfo will break ts-checker-rspack-plugin
    mode: 'readonly',
    // enable build when using project reference
    build: useReference,
    // avoid OOM issue
    memoryLimit: 8192,
    // use tsconfig of user project
    configFile: tsconfigPath,
    // resolve the default TypeScript package from user project
    resolveRoot: rootPath,
  },
  issue: {
    // ignore type errors from node_modules
    exclude: [({ file = '' }) => /[\\/]node_modules[\\/]/.test(file)],
  },
  logger: {
    log() {
      // do nothing
      // we only want to display error messages
    },
    error(message: string) {
      console.error(
        message
          .replace(/ERROR/g, 'Type Error')
          .replace(/WARNING/g, 'Type Warning'),
      );
    },
  },
},

默认情况下,Modern.js 会开启 @rsbuild/plugin-type-check 进行类型检查。你可以通过 output.disableTsChecker 配置项来关闭类型检查。

示例

Object 类型

tsChecker 的值为 Object 类型时,会与默认配置进行深层合并。

export default {
  tools: {
    tsChecker: {
      issue: {
        exclude: [({ file = '' }) => /[\\/]some-folder[\\/]/.test(file)],
      },
    },
  },
};

Function 类型

tsChecker 的值为 Function 类型时,默认配置会作为第一个参数传入。你可以直接修改配置对象,也可以返回一个对象作为最终配置。

export default {
  tools: {
    tsChecker(options) {
      options.async = false;
      return options;
    },
  },
};

请参考 @rsbuild/plugin-type-check 了解更多用法。

TypeScript 7+ 支持

tools.tsChecker 支持使用 TypeScript 7+ 的原生检查器进行类型检查。该能力由 @rsbuild/plugin-type-check 底层集成的 ts-checker-rspack-plugin 提供,可以减少大型项目的类型检查耗时。

安装 TypeScript >= 7.0.0 后会自动启用 tsgo

npm
yarn
pnpm
bun
deno
npm install typescript@latest -D

开启 tsgo 后,如果手动设置 typescript.typescriptPath,它必须指向 TypeScript 7+ 的 typescript/package.json 或旧版 @typescript/native-preview/package.json 的绝对路径。

@typescript/native-preview 路径仅作为兼容保留。新项目应使用标准 typescript 包提供的 TypeScript 7+。

关于 tsgo 模式下生效的配置项和相关限制,请参考 ts-checker-rspack-plugin - TypeScript 7+ support