# @definitelytyped/dtslint 中文文档

规范地址：https://npmcn.com/package/%40definitelytyped/dtslint/

版本：0\.2\.53
npm 元数据更新时间：2026-11-11T19:18:49.996Z
中文译文更新时间：2026-11-11T21:00:10.528Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40definitelytyped/dtslint
原文对照：https://npmcn.com/package/%40definitelytyped/dtslint/?lang=original
项目主页：https://github.com/microsoft/DefinitelyTyped-tools#readme
源码仓库：https://github.com/microsoft/DefinitelyTyped-tools

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：159 kB，27 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- Node.js 要求：^22\.22\.2 \|\| ^24\.15\.0 \|\| \>=26
- 运行依赖：16 个
- 首次发布：2021-12-01T16:34:48.064Z
- 最新版本发布：2026-11-11T19:18:49.789Z
- 近 30 天下载量：43985（2026-10-12 至 2026-11-10，npm 官方下载统计）
- 已发布版本数：271（近 12 个月 17 个）

## 中文简介

运行 TypeScript 定义文件的测试

## 安装

```sh
npm install @definitelytyped/dtslint
```

也可以使用：`pnpm add @definitelytyped/dtslint`、`yarn add @definitelytyped/dtslint`、`bun add @definitelytyped/dtslint`

## README

`dtslint` 测试 TypeScript 声明文件的样式和正确性. 它会安装 `typescript` 财务报告和审定财务报表 `eslint` 用于测试类型定义的唯一工具.

林特规则新到dtslint记录在 [文档](https://npmcn.com/package/%40definitelytyped/dtslint/docs) 目录.

# 只是寻找一个类型测试工具?

如果您只是寻找 TypeScript 类型测试工具, 请使用:

-   [`tsd`](https://github.com/tsdjs/tsd)
-   [`eslint-plugin-expect-type`](https://github.com/JoshuaKGoldberg/eslint-plugin-expect-type)
-   [`tstyche`](https://github.com/tstyche/tstyche)

# 设置

如果你正在研究绝对Typed,读一下 [绝对是已读](https://github.com/DefinitelyTyped/DefinitelyTyped#readme)。 。 。 .

如果您正在用 TypeScript 写入库, 请不要使用 `dtslint`。 。 。 . 使用 [`--declaration`](http://www.typescriptlang.org/docs/handbook/compiler-options.html) 为您生成类型定义.

如果您是图书馆作者,请在下面阅读.

## 为库添加类型( 不在绝对类型上)

[`dts-gen`](https://github.com/Microsoft/dts-gen#readme) 可能有所帮助,但并不需要.

创建一个 `types` 目录。 (名称为任意. ) 添加 `"types": "types"` 给您的 `package.json`。 。 。 . 有关捆绑类型的更多信息 [这里](http://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html)。 。 。 .

#### `types/index.d.ts`

仅 `index.d.ts` 财务报告和审定财务报表 `package.json` 需要向国家预防机制公布。 其他文件需要绝对输入测试. 将您的类型定义写入 `index.d.ts`。 。 。 . 参见 [手册](http://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html) 或者说 `dts-gen`如何做到这一点的模板 .

#### `types/tsconfig.json`

```json5
{
    "compilerOptions": {
        "module": "commonjs",
        "lib": [
            "es6",
        ],
        "noImplicitAny": true,
        "noImplicitThis": true,
        "strictNullChecks": true,
        "strictFunctionTypes": true,
        "types": [],
        "noEmit": true,
        "forceConsistentCasingInFileNames": true
    },
    "files": [
        "index.d.ts",
        "PACKAGE-NAME-tests.ts",
    ]
}
```

你可以延长 `"lib"` 例如, `["es6", "dom"]` 如果你需要这些打字. 您可能还需要添加 `"target": "es6"` 如果使用某些语言特性.

#### `types/package.json`

```json5
{
    "private": true,
    "name": "@types/PACKAGE-NAME",
    "version": "1.2.9999",
    "projects": [
        "https://example.com/"
    ],
    "dependencies": {
        "@types/DEPENDENCY-1": "*",
        "@types/DEPENDENCY-2": "*"
    },
    "devDependencies": {
        "@types/PACKAGE-NAME": "workspace:."
    },
    "owners": [
        {
            "name": "My Self",
            "githubUsername": "ghost"
        }
    ]
}
```

1.  如果类型是用于范围化的软件包,则必须命名-angle `@scope/package` 改为 `@types/scope__package`。 。 。 .
2.  主要版本和次要版本应该与一些已公布的npm包相匹配. 补丁版本必须是9999;绝对Typed会从0起递增已公布的补丁版本,类型补丁版本不会与npm包的补丁版本相匹配.
3.  `"projects"` 是源项目的一个链接.
4.  如果 _类型_ 除了标准类型以外,别指望任何东西.
5.  `"devDependencies"` 必须包含自我参考,例如: `"@types/PACKAGE-NAME": "workspace:.`。加上任何仅通过测试使用的依赖性.
6.  无`@types` 依赖性必须添加到 `allowedPackageJsonDependencies.txt` 在定义-parser 包中。 PR必须经打字组成员批准.
7.  如果您没有 github 用户名, 您可以提供 `"url"` 换成你自己.

还:

对于没有匹配的国家预防机制软件包的类型,增加两个属性:

1.  `"nonNpm": true`
2.  `"nonNpmDescription"`,为正在被打出的项目的可读取名称.

#### `types/.eslintrc.json`

一个 `.eslintrc.json` 文件是可选的. 如果您不需要修改任何规则设置, 可以跳过它 . 我们建议不增加 `.eslintrc.json` 文档.

```json5
{ "extends": "@definitelytyped/dtslint/dt.json" }
```

如果存在, 这将覆盖 `dtslint`'默认'[`all`](https://github.com/microsoft/DefinitelyTyped-tools/blob/main/packages/eslint-plugin/src/configs/all.ts)" 相会. 您可以指定新的 [规则](https://eslint.org/docs/latest/rules/),或禁用一些。 一个例子是:

```json5
{
    "rules": {
        "@definitelytyped/no-unnecessary-generics": "off"
    }
}
```

请别没有正当理由就这么做. 无效规则使得绝对打字的公关不太可能被合并,而且肯定需要更长的时间进行审查.

#### `types/test.ts`

您可以拥有任何数量的测试文件, 以及任何名字 。 参见以下内容:.

## 写作测试

测试文件应当是一个使用库进行测试的样本代码. 测试经过打字检查,但不运行. 断言某一表达式是特定类型的,使用 `$ExpectType`。 。 。 . 断言表达式会导致编译错误,使用 `@ts-expect-error`。 。 。 . (答复将予核对) `expect` 灵通通. )

```ts
import { f } from "my-lib"; // f is(n: number) => void

// $ExpectType void
f(1);

// Can also write the assertion on the same line (but not if it's a multiline function call).
f(2); // $ExpectType void

// @ts-expect-error
f("one");
```

## 指定类型脚本版本

通常的包件将按 [当然 类型支持窗口](https://github.com/DefinitelyTyped/DefinitelyTyped#support-window)。 。 。 . 要将测试仅限于新版本,请在包中指定. json:

```ts
"minimumTypeScriptVersion: 5.6"
```

这种测试只有5.6及以上,虽然人们如果愿意,仍然可以依赖下版Typescript的包.

## 运行测试

-   `npm install --save-dev @definitelytyped/dtslint`
-   添加到您的 `package.json` `scripts`数字 : `"dtslint": "dtslint types"`
-   `npm run dtslint`

### 选项

-   `--localTs`

使用本地安装的 TypeScript 版本 .

```sh
dtslint --localTs node_modules/typescript/lib types
```

对于TypeScript 7,在建立本土编译器和JavaScript API后,通过TypeScript包目录:

```sh
dtslint --localTs ../TypeScript/packages/typescript types
```

-   `--expectOnly`

禁用除类型正确性检查规则以外的所有规则 .

```sh
dtslint --expectOnly types
```

# 捐款

## 构建

```sh
npm link . # Global 'dts-lint' should now refer to this.
npm run watch
```

## 测试

使用 `pnpm test` 运行所有测试.

## 行为守则

该项目通过了 [Microsoft 开源行为守则](https://opensource.microsoft.com/codeofconduct/)。更多信息见 [《行为守则》](https://opensource.microsoft.com/codeofconduct/faq/) 或联系人 [打开码@ microsoft.com](mailto:opencode@microsoft.com) 并附上任何其他问题或评论.

## 财务问题

我得到一个错误 关于缺失的打字装置.

```
Error: Cannot find module '/node_modules/dtslint/typescript-installs/3.1/node_modules/typescript`
```

您的附属关系可能已经过时 . [@ deminitetyped/typescript-versions (英语)](https://github.com/microsoft/DefinitelyTyped-tools/tree/main/packages/typescript-versions) 是包含要安装的 TypeScript 版本列表的软件包.

或者,如果dtslint在每次运行中都是从头安装,那么这种错误可能是由于同时引用dtslint来践踏对方的TypeScript设施造成的,特别是在连续集成的情况下. 若您使用的话 [莱娜](https://github.com/lerna/lerna/tree/main/commands/run#readme),尝试运行 dtslint 与 [`lerna --concurrency 1 run ...`](https://github.com/lerna/lerna/tree/main/core/global-options#--concurrency)。 。 。 .

## 常见问题

### @definitelytyped/dtslint 是什么？

运行 TypeScript 定义文件的测试

### 如何安装 @definitelytyped/dtslint？

在项目目录运行 npm install @definitelytyped/dtslint，或使用 pnpm add / yarn add / bun add。它还提供命令行工具 dtslint，可以用 npx dtslint 直接运行。

### @definitelytyped/dtslint 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @definitelytyped/dtslint 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @definitelytyped/dtslint 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js ^22\.22\.2 \|\| ^24\.15\.0 \|\| \>=26。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @definitelytyped/dtslint 可以商用吗？

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

### @definitelytyped/dtslint 的下载量有多少？

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

### @definitelytyped/dtslint 还在维护吗？

最新版本 0\.2\.53 发布于 2026/11/12（今天）；至今共发布 271 个版本，其中过去 12 个月发布了 17 个；首次发布于 2021/12/02。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

