typescript 从javascript生成打字稿声明文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18301898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:08:13  来源:igfitidea点击:

Generating typescript declaration files from javascript

typescript

提问by Games Brainiac

Say for example, you have a npm library, in my case mongoose, how would you go about generating d.tsfiles?

比如说,你有一个 npm 库,就我而言mongoose,你将如何生成d.ts文件?

回答by Fenton

JavaScript doesn't always contain enough type information for the TypeScript compiler to infer the structures in your code - so automatically generating a definition based on JavaScript is rarely an option.

JavaScript 并不总是包含足够的类型信息供 TypeScript 编译器推断代码中的结构 - 因此基于 JavaScript 自动生成定义很少是一种选择。

There are instructions on how to write them from scratch here:

这里有关于如何从头开始编写它们的说明:

https://www.stevefenton.co.uk/2013/01/complex-typescript-definitions-made-easy/

https://www.stevefenton.co.uk/2013/01/complex-typescript-definitions-made-easy/

But there is one trick that might work (it only works in a limited set of cases).

但是有一个技巧可能有效(它只在有限的情况下有效)。

If you paste the JavaScript into a new TypeScript file, fix any trivial errors you may get and compile it using the definition flag, it may be able to get you a file that would at least be a starting point.

如果您将 JavaScript 粘贴到新的 TypeScript 文件中,修复您可能遇到的任何小错误并使用定义标志编译它,它可能会为您提供一个至少可以作为起点的文件。

tsc --declaration js.ts

回答by Meligy

The question is a bit old, but for the sake of people coming from search engines like me, if you are looking for an automated tool, check out:

这个问题有点老了,但是为了像我这样来自搜索引擎的人,如果您正在寻找自动化工具,请查看:

  • Microsoft/dts-gen

    The official starting point Microsoft uses when creating types. It's meant to be a starting point only though and I didn't get a lot of luck depending just on it

  • dtsmake

    This one looks very promising. It depends on Ternjs which some editors use to provide autocomplete for JS code.

    Check it out also for other tool names and comparisons with them.

  • 微软/dts-gen

    Microsoft 在创建类型时使用的官方起点。这只是一个起点,我并没有因为它而获得很多运气

  • dtsmake

    这个看起来很有希望。这取决于一些编辑器用来为 JS 代码提供自动完成功能的 Ternjs。

    还可以查看其他工具名称并与它们进行比较。

回答by Joost de Vries

For other people who find this post through google: there's a generator now by Microsoft itself: dts-gen.

对于通过谷歌找到这篇文章的其他人:微软自己现在有一个生成器:dts-gen

回答by Steve Brush

If you're attempting to use a third-party JavaScript library in your TypeScript file, you can create a custom (and nearly blank) declaration file anywhere in your project.

如果您尝试在 TypeScript 文件中使用第三方 JavaScript 库,则可以在项目的任何位置创建自定义(且几乎是空白的)声明文件。

For example, if you wanted to import:

例如,如果您想导入:

import * as fooLibrary from 'foo-lib';

You would create a new file named 'foo-lib.d.ts' with the following contents:

您将创建一个名为“foo-lib.d.ts”的新文件,内容如下:

declare module 'foo-lib' {
  var fooLibrary: any;
  export = fooLibrary;
}

回答by tarling

For my particular situation, where I was working with obfuscated code from a third party, I found it useful to load the script in a page, and then use the console to log an instance of the obfuscated class. The console gives you a neat summary of the class methods and properties which you can copy and use as the starting point of a definition file.

对于我处理来自第三方的混淆代码的特殊情况,我发现在页面中加载脚本很有用,然后使用控制台记录混淆类的实例。控制台为您提供了类方法和属性的简洁摘要,您可以将它们复制并用作定义文件的起点。

> o = new ObfuscatedClass()
> console.log(o)
ObfuscatedClass
- methodA(a,b){some implementation}
- methodB(a,b){other implementation} etc

which you can copy paste and edit to

您可以复制粘贴并编辑到

declare class ObfuscatedClass {

    methodA(a,b);
    methodB(a,b);

}

回答by Vytenis Urbonavi?ius

You might want to have a look at "npm-dts" NPM package. It is a tool for generating single index.d.ts file for your NPM package so that it could be consumed by other TypeScript packages out of the box.

你可能想看看“ npm-dts” NPM 包。它是一个为 NPM 包生成单个 index.d.ts 文件的工具,以便其他 TypeScript 包开箱即用。