@definitelytyped/dtslint

中文文档
0.2.53发布于 2026/11/12MITTypeScriptCommonJS近 30 天下载 4.4万

Runs tests on TypeScript definition files

原始使用文档
文档目录9 节
  1. Add types for a library (not on DefinitelyTyped)
  2. Write tests
  3. Specify a TypeScript version
  4. Run tests
  5. Options
  6. Build
  7. Test
  8. Code of Conduct
  9. FAQ

dtslint tests a TypeScript declaration file for style and correctness. It will install typescript and eslint for you, so this is the only tool you need to test a type definition.

Lint rules new to dtslint are documented in the docs directory.

Just looking for a type testing tool?

If you are just looking for a TypeScript type testing tool, use:

Setup

If you are working on DefinitelyTyped, read the DefinitelyTyped README.

If you are writing the library in TypeScript, don't use dtslint. Use --declaration to have type definitions generated for you.

If you are a library author, read below.

Add types for a library (not on DefinitelyTyped)

dts-gen may help, but is not required.

Create a types directory. (Name is arbitrary.) Add "types": "types" to your package.json. Read more on bundling types here.

types/index.d.ts

Only index.d.ts and package.json need to be published to NPM. The other files will be required by Definitely Typed for testing. Write your type definitions in index.d.ts. Refer to the handbook or dts-gen's templates for how to do this.

types/tsconfig.json

{
    "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",
    ]
}

You may extend "lib" to, for example, ["es6", "dom"] if you need those typings. You may also have to add "target": "es6" if using certain language features.

types/package.json

{
    "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. If the types are for a scoped package, you must name-mangle @scope/package to @types/scope__package.
  2. The major and minor versions should match some published version of the npm package. The patch version must be 9999; Definitely Typed will increment published patch versions starting at 0, and the patch version of the types will not match the patch version of the npm package.
  3. "projects" is a link to the source project.
  4. There might not be any dependencies if the types don't rely on anything but standard types.
  5. "devDependencies" must include a self-reference like "@types/PACKAGE-NAME": "workspace:.. Plus any dependencies used only by tests.
  6. Non-@types dependencies must be added to allowedPackageJsonDependencies.txt in the definitions-parser package. The PR must be approved by a Typescript team member.
  7. If you do not have a github user name, you can provide a "url" of your own instead.

Also:

For types that do not have a matching NPM package, add two properties:

  1. "nonNpm": true
  2. "nonNpmDescription", a human-readable name for the project that is being typed.

types/.eslintrc.json

An .eslintrc.json file is optional. You can skip it if you don't need to modify any lint rule settings. We recommend not adding an .eslintrc.json file.

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

If present, this will override dtslint's default "all" config. You can specify new lint rules, or disable some. An example:

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

Please don't do this without a good reason. Disabling lint rules makes a Definitely Typed PR less likely to be merged, and will definitely take longer to review.

types/test.ts

You can have any number of test files you want, with any names. See below on what to put in them.

Write tests

A test file should be a piece of sample code that tests using the library. Tests are type-checked, but not run. To assert that an expression is of a given type, use $ExpectType. To assert that an expression causes a compile error, use @ts-expect-error. (Assertions will be checked by the expect lint rule.)

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");

Specify a TypeScript version

Normally packages will be tested according to DefinitelyType's support window. To restrict testing to new versions only, specify it in package.json:

"minimumTypeScriptVersion: 5.6"

This tests only 5.6 and above, although people can still depend on the package with lower versions of Typescript if they want.

Run tests

  • npm install --save-dev @definitelytyped/dtslint
  • Add to your package.json scripts: "dtslint": "dtslint types"
  • npm run dtslint

Options

  • --localTs

Use your locally installed version of TypeScript.

dtslint --localTs node_modules/typescript/lib types

For TypeScript 7, pass the TypeScript package directory after building both the native compiler and JavaScript API:

dtslint --localTs ../TypeScript/packages/typescript types
  • --expectOnly

Disable all the lint rules except the one that checks for type correctness.

dtslint --expectOnly types

Contributing

Build

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

Test

Use pnpm test to run all tests.

Code of Conduct

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

FAQ

I'm getting an error about a missing typescript install.

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

Your dependencies may be out of date. @definitelytyped/typescript-versions is the package that contains the list of TypeScript versions to install.

Alternatively this error can be caused by concurrent dtslint invocations trampling each other's TypeScript installations, especially in the context of continuous integration, if dtslint is installed from scratch in each run. If for example you use Lerna, try running dtslint with lerna --concurrency 1 run ....

版本与发布记录

@definitelytyped/dtslint 共发布 271 个版本,首次发布于 2021/12/02;过去 12 个月发布了 17 个版本。

版本发布日期距今
0.2.53latest2026/11/12今天
0.2.522026/11/057 天前
0.2.512026/11/047 天前
0.2.492026/10/2221 天前
0.2.472026/07/213 个月前
0.2.462026/07/094 个月前

其他发布通道:next → 0.0.175-next.1。

关于 @definitelytyped/dtslint 的常见问题

@definitelytyped/dtslint 是什么?

Runs tests on TypeScript definition files

如何安装 @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 和提交记录判断。