dtslint 测试 TypeScript 声明文件的样式和正确性.
它会安装 typescript 财务报告和审定财务报表 eslint 用于测试类型定义的唯一工具.
林特规则新到dtslint记录在 文档 目录.
只是寻找一个类型测试工具?
如果您只是寻找 TypeScript 类型测试工具, 请使用:
设置
如果你正在研究绝对Typed,读一下 绝对是已读。 。 。 .
如果您正在用 TypeScript 写入库, 请不要使用 dtslint。 。 。 .
使用 --declaration 为您生成类型定义.
如果您是图书馆作者,请在下面阅读.
为库添加类型( 不在绝对类型上)
dts-gen 可能有所帮助,但并不需要.
创建一个 types 目录。 (名称为任意. )
添加 "types": "types" 给您的 package.json。 。 。 .
有关捆绑类型的更多信息 这里。 。 。 .
types/index.d.ts
仅 index.d.ts 财务报告和审定财务报表 package.json 需要向国家预防机制公布。 其他文件需要绝对输入测试.
将您的类型定义写入 index.d.ts。 。 。 .
参见 手册 或者说 dts-gen如何做到这一点的模板 .
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",
]
}
你可以延长 "lib" 例如, ["es6", "dom"] 如果你需要这些打字.
您可能还需要添加 "target": "es6" 如果使用某些语言特性.
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"
}
]
}
- 如果类型是用于范围化的软件包,则必须命名-angle
@scope/package改为@types/scope__package。 。 。 . - 主要版本和次要版本应该与一些已公布的npm包相匹配. 补丁版本必须是9999;绝对Typed会从0起递增已公布的补丁版本,类型补丁版本不会与npm包的补丁版本相匹配.
"projects"是源项目的一个链接.- 如果 类型 除了标准类型以外,别指望任何东西.
"devDependencies"必须包含自我参考,例如:"@types/PACKAGE-NAME": "workspace:.。加上任何仅通过测试使用的依赖性.- 无
@types依赖性必须添加到allowedPackageJsonDependencies.txt在定义-parser 包中。 PR必须经打字组成员批准. - 如果您没有 github 用户名, 您可以提供
"url"换成你自己.
还:
对于没有匹配的国家预防机制软件包的类型,增加两个属性:
"nonNpm": true"nonNpmDescription",为正在被打出的项目的可读取名称.
types/.eslintrc.json
一个 .eslintrc.json 文件是可选的.
如果您不需要修改任何规则设置, 可以跳过它 .
我们建议不增加 .eslintrc.json 文档.
{ "extends": "@definitelytyped/dtslint/dt.json" }
如果存在, 这将覆盖 dtslint'默认'all" 相会.
您可以指定新的 规则,或禁用一些。 一个例子是:
{
"rules": {
"@definitelytyped/no-unnecessary-generics": "off"
}
}
请别没有正当理由就这么做. 无效规则使得绝对打字的公关不太可能被合并,而且肯定需要更长的时间进行审查.
types/test.ts
您可以拥有任何数量的测试文件, 以及任何名字 。 参见以下内容:.
写作测试
测试文件应当是一个使用库进行测试的样本代码. 测试经过打字检查,但不运行.
断言某一表达式是特定类型的,使用 $ExpectType。 。 。 .
断言表达式会导致编译错误,使用 @ts-expect-error。 。 。 .
(答复将予核对) expect 灵通通. )
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");
指定类型脚本版本
通常的包件将按 当然 类型支持窗口。 。 。 . 要将测试仅限于新版本,请在包中指定. json:
"minimumTypeScriptVersion: 5.6"
这种测试只有5.6及以上,虽然人们如果愿意,仍然可以依赖下版Typescript的包.
运行测试
npm install --save-dev @definitelytyped/dtslint- 添加到您的
package.jsonscripts数字 :"dtslint": "dtslint types" npm run dtslint
选项
--localTs
使用本地安装的 TypeScript 版本 .
dtslint --localTs node_modules/typescript/lib types
对于TypeScript 7,在建立本土编译器和JavaScript API后,通过TypeScript包目录:
dtslint --localTs ../TypeScript/packages/typescript types
--expectOnly
禁用除类型正确性检查规则以外的所有规则 .
dtslint --expectOnly types
捐款
构建
npm link . # Global 'dts-lint' should now refer to this.
npm run watch
测试
使用 pnpm test 运行所有测试.
行为守则
该项目通过了 Microsoft 开源行为守则。更多信息见 《行为守则》 或联系人 打开码@ microsoft.com 并附上任何其他问题或评论.
财务问题
我得到一个错误 关于缺失的打字装置.
Error: Cannot find module '/node_modules/dtslint/typescript-installs/3.1/node_modules/typescript`
您的附属关系可能已经过时 . @ deminitetyped/typescript-versions (英语) 是包含要安装的 TypeScript 版本列表的软件包.
或者,如果dtslint在每次运行中都是从头安装,那么这种错误可能是由于同时引用dtslint来践踏对方的TypeScript设施造成的,特别是在连续集成的情况下.
若您使用的话 莱娜,尝试运行 dtslint 与 lerna --concurrency 1 run ...。 。 。 .