您如何生成 .d.ts“打字"?来自现有 JavaScript 库的定义文件? [英] How do you produce a .d.ts "typings" definition file from an existing JavaScript library?

查看:25
本文介绍了您如何生成 .d.ts“打字"?来自现有 JavaScript 库的定义文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了很多我自己的和第三方的库.我看到typings"目录包含一些用于 Jquery 和 WinRT 的……但它们是如何创建的?

I'm using a lot of libraries both my own and 3rd party. I see the "typings" directory contains some for Jquery and WinRT... but how are they created?

推荐答案

根据相关库、它的编写方式以及您正在寻找的准确度级别,您可以使用一些选项.让我们大致按照需求的降序来回顾一下这些选项.

There are a few options available for you depending on the library in question, how it's written, and what level of accuracy you're looking for. Let's review the options, in roughly descending order of desirability.

始终首先检查绝对类型(https://github.com/DefinitelyTyped/DefinitelyTyped).这是一个充满了数以千计的 .d.ts 文件的社区存储库,很可能您正在使用的东西已经存在.您还应该检查 TypeSearch (https://microsoft.github.io/TypeSearch/),这是一个NPM 发布的 .d.ts 文件的搜索引擎;这将比绝对类型有更多的定义.一些模块也将自己的定义作为其 NPM 发行版的一部分发布,因此在尝试编写自己的模块之前,请先看看是否是这种情况.

Always check DefinitelyTyped (https://github.com/DefinitelyTyped/DefinitelyTyped) first. This is a community repo full of literally thousands of .d.ts files and it's very likely the thing you're using is already there. You should also check TypeSearch (https://microsoft.github.io/TypeSearch/) which is a search engine for NPM-published .d.ts files; this will have slightly more definitions than DefinitelyTyped. A few modules are also shipping their own definitions as part of their NPM distribution, so also see if that's the case before trying to write your own.

TypeScript 现在支持 --allowJs 标志,并将在 .js 文件中进行更多基于 JS 的推断.您可以尝试在编译中包含 .js 文件以及 --allowJs 设置,以查看这是否为您提供了足够好的类型信息.TypeScript 会识别这些文件中的 ES5 样式类和 JSDoc 注释等内容,但如果库以一种奇怪的方式初始化自己,则可能会被绊倒.

TypeScript now supports the --allowJs flag and will make more JS-based inferences in .js files. You can try including the .js file in your compilation along with the --allowJs setting to see if this gives you good enough type information. TypeScript will recognize things like ES5-style classes and JSDoc comments in these files, but may get tripped up if the library initializes itself in a weird way.

如果 --allowJs 给了你不错的结果,并且你想自己编写一个更好的定义文件,你可以将 --allowJs--declaration 以查看 TypeScript 对库类型的最佳猜测".这将为您提供一个不错的起点,如果 JSDoc 注释写得很好并且编译器能够找到它们,那么它可能与手工编写的文件一样好.

If --allowJs gave you decent results and you want to write a better definition file yourself, you can combine --allowJs with --declaration to see TypeScript's "best guess" at the types of the library. This will give you a decent starting point, and may be as good as a hand-authored file if the JSDoc comments are well-written and the compiler was able to find them.

如果 --allowJs 不起作用,您可能需要使用 dts-gen (https://github.com/Microsoft/dts-gen) 以获得起点.此工具使用对象的运行时形状来准确枚举所有可用属性.从好的方面来说,这往往非常准确,但该工具尚不支持抓取 JSDoc 注释以填充其他类型.你像这样运行:

If --allowJs didn't work, you might want to use dts-gen (https://github.com/Microsoft/dts-gen) to get a starting point. This tool uses the runtime shape of the object to accurately enumerate all available properties. On the plus side this tends to be very accurate, but the tool does not yet support scraping the JSDoc comments to populate additional types. You run this like so:

npm install -g dts-gen
dts-gen -m <your-module>

这将在当前文件夹中生成 your-module.d.ts.

This will generate your-module.d.ts in the current folder.

如果你只是想稍后再做,暂时不用类型,那么在 TypeScript 2.0 中你现在可以编写

If you just want to do it all later and go without types for a while, in TypeScript 2.0 you can now write

declare module "foo";

这会让你import "foo" 类型为any 的模块.如果你有一个想要稍后处理的全局,就写

which will let you import the "foo" module with type any. If you have a global you want to deal with later, just write

declare const foo: any;

它会给你一个 foo 变量.

which will give you a foo variable.

这篇关于您如何生成 .d.ts“打字"?来自现有 JavaScript 库的定义文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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