@ava/typescript

中文文档
7.0.0发布于 2026/04/17MITES Module构建来源已验证近 30 天下载 16.6万

TypeScript provider for AVA

原始使用文档
文档目录3 节
  1. Enabling TypeScript support
  2. ES Modules
  3. Add additional extensions

@ava/typescript

Adds TypeScript support to AVA.

This is designed to work for projects that precompile TypeScript. It allows AVA to load the compiled JavaScript, while configuring AVA to treat the TypeScript files as test files.

In other words, say you have a test file at src/test.ts. You've configured TypeScript to output to build/. Using @ava/typescript you can run the test using npx ava src/test.ts.

Compatible with AVA 6, 7 and 8. Requires Node.js 22.20, 24.12 or 25 (or newer).

Enabling TypeScript support

Add this package to your project:

npm install --save-dev @ava/typescript

Then, enable TypeScript support either in package.json or ava.config.*:

package.json:

{
    "ava": {
        "typescript": {
            "rewritePaths": {
                "src/": "build/"
            },
            "compile": false
        }
    }
}

Both keys and values of the rewritePaths object must end with a /. Paths are relative to your project directory.

You can enable compilation via the compile property. If false, AVA will assume you have already compiled your project. If set to 'tsc', AVA will run the TypeScript compiler before running your tests. This can be inefficient when using AVA in watch mode.

Output files are expected to have the .js extension.

AVA searches your entire project for *.js, *.mjs, *.ts and *.mts files﹡ (or other extensions you've configured). It will ignore such files found in the rewritePaths targets (e.g. build/). If you use more specific paths, for instance build/main/, you may need to change AVA's files configuration to ignore other directories.

﹡ AVA 6 and 7 will also search for *.cjs and *.cts files.

ES Modules

With AVA 6 and 7, if your package.json has configured "type": "module", or you've configured AVA to treat the js extension as module, then @ava/typescript will import the output file as an ES module. Note that this is based on the output file, not the ts extension.

AVA 8 will always import all files as ES modules.

Add additional extensions

You can configure AVA to recognize additional file extensions. To add (partial†) JSX support:

package.json:

{
    "ava": {
        "typescript": {
            "extensions": ["ts", "tsx"],
            "rewritePaths": {
                "src/": "build/"
            }
        }
    }
}

If you use the allowJs TypeScript option you'll have to specify the js, cjs and mjs extensions for them to be rewritten.

See also AVA's extensions option.

† Note that the preserve mode for JSX is not (yet) supported.

版本与发布记录

@ava/typescript 共发布 11 个版本,首次发布于 2020/01/26;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
7.0.0latest2026/04/176 个月前
6.0.02025/06/181.4 年前
5.0.02024/05/062 年前
4.1.02023/07/013 年前
4.0.02023/03/133 年前
3.0.12021/11/174 年前

关于 @ava/typescript 的常见问题

@ava/typescript 是什么?

TypeScript provider for AVA

如何安装 @ava/typescript?

在项目目录运行 npm install @ava/typescript,或使用 pnpm add / yarn add / bun add。

@ava/typescript 支持 TypeScript 吗?

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

@ava/typescript 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@ava/typescript 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js ^22.20 || ^24.12 || >=25。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@ava/typescript 可以商用吗?

@ava/typescript 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ava/typescript 的下载量有多少?

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

@ava/typescript 还在维护吗?

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