typescript tsd:安装本地定义文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34569659/
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
tsd: install local definition file
提问by Simon
I have a local node package written in TypeScript, which I want to use in my actual project. Using npm, I can install local packages like this:
我有一个用 TypeScript 编写的本地节点包,我想在我的实际项目中使用它。使用 npm,我可以像这样安装本地包:
$ npm install --save /path/to/package
Or:
或者:
$ npm install --save /path/to/package.tar.gz
This installs the required .js files in the node_modules directory. There's also a generated .d.ts file within that package, which I'd like to install to my project (automatically linking it in typings/tsd.d.ts). But using the following command has no effect:
这会在 node_modules 目录中安装所需的 .js 文件。该包中还有一个生成的 .d.ts 文件,我想将其安装到我的项目中(自动将它链接到typings/tsd.d.ts)。但是使用下面的命令没有效果:
$ tsd install /path/to/package/package.d.ts --save
It says >> zero results
. So, what is the way to install local definition files without the need of a repository?
它说>> zero results
。那么,在不需要存储库的情况下安装本地定义文件的方法是什么?
UPDATE:
更新:
I can simply copy my d.ts file into the typings directory and my text editor (for me it's Sublime Text with the TypeScript plugin) it's able to find the declaration. The directory layout is something like this:
我可以简单地将我的 d.ts 文件复制到typings 目录和我的文本编辑器(对我来说它是带有 TypeScript 插件的 Sublime Text)它能够找到声明。目录布局是这样的:
/my-project/
/typings/
tsd.d.ts - auto-generated by `tsd install`
node/ - I've installed the node definitions
my-package.d.ts - copied or symlinked file
my-project.ts - I'm working here
However I've got an issue when exporting the only function in module.exports
(exports = function...
in TypeScript). In this case, the exported function is kinda 'anonymous' and isn't even named in the d.ts file, so I need to edit it manually.
但是,我在导出module.exports
(exports = function...
在 TypeScript 中) 中的唯一函数时遇到了问题。在这种情况下,导出的函数有点“匿名”,甚至没有在 d.ts 文件中命名,所以我需要手动编辑它。
My test case:
我的测试用例:
'my-package' provides a single function, usually imported as 'myPackage':
'my-package' 提供一个函数,通常作为 'myPackage' 导入:
export = function myPackage(a: string, b: string) { return a + ' ' + b; };
declaration
is set to true
in tsconfig.json, so the tsc
command generated a my-package.d.ts file:
declaration
true
在 tsconfig.json 中设置为,因此该tsc
命令生成了一个 my-package.d.ts 文件:
declare var _default: (a: string, b: string) => string;
export = _default;
My package is supposed to be used like this in my project:
我的包应该在我的项目中这样使用:
import myPackage = require('my-package');
myPackage('foo', 'bar');
However, tsc can't find myPackage
, even though my-package.d.ts
was copied into the typings folder. I need to edit that file so it looks like this:
但是, tsc 找不到myPackage
,即使my-package.d.ts
已复制到typings 文件夹中。我需要编辑该文件,使其看起来像这样:
declare var myPackage: (a: string, b: string) => string;
//export = _default; - not needed
Or even better for a correct functioning require()
:
或者甚至更好地正常运行require()
:
declare module 'my-package' /* this is the string passed to require() */ {
export = function(a: string, b: string): string;
}
采纳答案by David Sherret
In your local node package, add a typescript > definition
entry in package.json
:
在您的本地节点包中,添加一个typescript > definition
条目package.json
:
{
"name": "your-package",
...
"typescript": {
"definition": "package.d.ts"
}
}
Then after installing the package in your project, run the command...
然后在您的项目中安装软件包后,运行命令...
tsd link
...which will add a reference to package.d.ts
in your project's tsd.d.ts
file (reference).
...这将package.d.ts
在您的项目tsd.d.ts
文件 ( reference) 中添加一个引用。
Also, based on your edit, I would suggest you change your definition file to something like this (note the quotes around my-package
):
此外,根据您的编辑,我建议您将定义文件更改为这样的内容(注意周围的引号my-package
):
declare module "my-package" {
function myPackage(a: string, b: string): string;
export = myPackage;
}
That will make it work with the following code:
这将使它与以下代码一起工作:
import myPackage = require('my-package');
myPackage('foo', 'bar');
回答by maxime1992
Even if the trick with package.json is working, I rather prefer the tools made for that (tsd or typings).
即使 package.json 的技巧有效,我还是更喜欢为此制作的工具(tsd 或typings)。
I just found the answer for Typings :typings install --save --ambient file:./node_modules/.../file.d.ts
我刚刚找到了 Typings 的答案:typings install --save --ambient file:./node_modules/.../file.d.ts
I think it's the same with tsd :)
我认为这与 tsd 相同:)
EDIT:
Since TypeScript 2.0 typings is useless.
Just run npm i --save-dev @types/some-library
编辑:
由于 TypeScript 2.0 打字是无用的。
赶紧跑npm i --save-dev @types/some-library
回答by Hongbin HU
As of TypeScript 1.6 you can reference your type definition file from you package.json and TypeScript's module resolution should be able to dig out the type definition.
从 TypeScript 1.6 开始,您可以从 package.json 中引用您的类型定义文件,并且 TypeScript 的模块解析应该能够挖掘出类型定义。
In your package.json file (of your local npm module), add a "typings" entry, e.g.
在您的 package.json 文件(本地 npm 模块的)中,添加一个“typings”条目,例如
{
"name": "my-package",
"typings": "./relative/path/to/my-package.d.ts"
}
This way you don't need to fiddle with TSD at all.
这样你根本不需要摆弄TSD。
See TypeScript Wiki: https://github.com/Microsoft/TypeScript/wiki/Typings-for-npm-packages
请参阅 TypeScript Wiki:https: //github.com/Microsoft/TypeScript/wiki/Typings-for-npm-packages