如何为打字稿打字定义编写测试? [英] How to write tests for typescript typing definition?

查看:30
本文介绍了如何为打字稿打字定义编写测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个 Node.js 库,最近我们为它添加了一个类型定义.

We have a Node.js lib, recently we added a type definitions for it.

但是我如何测试类型定义?

But how could I test the type definition?

推荐答案

DefinitelyTyped 中的测试是应该正确类型检查的文件.因此,测试会演练 API 的不同部分,并通过编译器运行(但实际上并未执行生成的 JavaScript 代码).例如,请参阅 @types/express<的测试/a>.

The tests in DefinitelyTyped are files that are supposed to type-check correctly. So the test exercises different parts of the API, and it's run through the compiler (but the generated JavaScript code is not actually executed). See, for example, tests for @types/express.

对于您自己的项目,我猜您会自己编写一个类似的测试文件,并将其编译为构建的一部分(如果编译成功,则测试成功).当然,如果您已经拥有使用这些类型定义的现有 TypeScript 代码,那可能就足够测试了.

For your own project, I guess you'd write a similar test file yourself, and compile it as part of your build (if it compiles, then the test succeeds). Of course, if you already have existing TypeScript code using those type definitions, that might be sufficient test.

Typings-checker 是一个概念验证,也允许测试失败(错误类型的代码不应编译).

Typings-checker is a proof-of-concept that also allows testing failures (incorrectly typed code should not compile).

这篇关于如何为打字稿打字定义编写测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆