首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在TypeScript v2.2.2中,如何为没有tsd的Javascript库创建定义文件?

在TypeScript v2.2.2中,如何为没有tsd的Javascript库创建定义文件?
EN

Stack Overflow用户
提问于 2017-04-20 00:28:26
回答 1查看 299关注 0票数 3

我正在搜索一个完整的示例,或者至少是为一个JavaScript库创建一个定义文件的最佳实践(不一定必须是一个tsd),该库没有@type中的定义文件或包中包含的定义文件。

到目前为止,我已经能够在这里上找到一个基本的解决方案,但是它似乎没有遵循TypeScript网站上的“定义”部分中给出的确切指导。

在上面的存储库中,我使用的是包@google-cloud/datastore,据我所知,它还没有tsd文件。这些文档足够好,我应该能够为我感兴趣的部分创建一个手动定义文件,但是我很难弄清楚手动导出定义的最佳实践是什么。

具体来说,从这里,它说:

代码语言:javascript
复制
/*~ This is the module template file for class modules.
 *~ You should rename it to index.d.ts and place it in a folder with the same name as the module.
 *~ For example, if you were writing a file for "super-greeter", this
 *~ file should be 'super-greeter/index.d.ts'
 */

/*~ Note that ES6 modules cannot directly export class objects.
 *~ This file should be imported using the CommonJS-style:
 *~   import x = require('someLibrary');
 *~
 *~ Refer to the documentation to understand common
 *~ workarounds for this limitation of ES6 modules.
 */

/*~ If this module is a UMD module that exposes a global variable 'myClassLib' when
 *~ loaded outside a module loader environment, declare that global here.
 *~ Otherwise, delete this declaration.
 */

根据上面的说明,听起来我应该能够将一个index.d.ts文件放到我的node_modules/@types目录之外的文件夹中,这个文件夹结构与我的模块相同。例如,在我的存储库中,我有一个顶级目录@google-cloud/datastore,其中包含一个index.ts文件,但是从文档中看,我应该能够使用index.d.ts文件;但是,我的模块和编译器都能够找到这个文件。我需要一个编译器选项吗?

第二,我在存储库中采用的方法是正确的。这种方式没有记录在他们的网站上,我不得不猜测,然后才能找到解决方案。我实际上是将JavaScript包google-cloud/datastore导入到index.ts中,添加我的类型,然后导出它以供我的模块使用。但是,我的模块必须引用./@google-cloud/datastore的包,因为这是TypeScript定义的实际位置。

简而言之,在TypeScript v2.2.2中,为不包含tsd的JavaScript库创建小型本地TypeScript定义文件的最佳实践是什么?

后果

我用亚历克斯建议的解决方案更新了我的存储库,一切似乎都如期而至。我想指出的是,它似乎也可以通过添加一个{ "baseUrl": "types" }参数来工作,但我不清楚为什么这样做是正确的,所以我采用了建议的方法。--traceResolution标志对此非常有用。

在这个示例中,我确保还包括另一个具有TypeScript定义的模块,lodash,以确保正常的模块分辨率仍然正确。

我的目录/文件结构:

代码语言:javascript
复制
+ js
  ...
+ node_modules
  + @google-cloud
    + datastore
  + lodash
  + @types
    + lodash
  + ...
+ types
  + @google-cloud
  - index.d.ts
  + datastore
    -  index.d.ts 
- index.ts
- package.json
- tsconfig.json

迹分辨

出于好奇,我想通过调用tsc --traceResolution发布一个错误/正确结果的例子。为了清楚起见,我把结果缩略了一下。

不正确的而不使用paths参数TypeScript将@google-cloud/datastore解析为index.js文件,这是错误的。

代码语言:javascript
复制
======== Module name '@google-cloud/datastore' was successfully resolved to 'C:/Users/Kory/Documents/Repos/playground/manual-typescript-definition/node_modules/@google-cloud/datastore/src/index.js'. ========

使用paths参数更正paths。注意,@google-cloud/datastore解析为index.d.ts文件,而不是JavaScript文件。

代码语言:javascript
复制
======== Module name '@google-cloud/datastore' was successfully resolved to 'C:/Users/Kory/Documents/Repos/playground/manual-typescript-definition/types/@google-cloud/datastore/index.d.ts'. ========
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-20 04:32:06

您可以使用compilerOptions pathstypeRoots获取tsc来检测自定义.d.ts文件。它将在列出的目录中搜索与您的包(即@google-cloud/datastore)匹配的文件夹。

例如

代码语言:javascript
复制
"paths": {
  "*": [
    "types/*"
  ]
},
"typeRoots": [
  "./types"
]

其中types是目录结构根目录下的一个文件夹,您可以创建一个文件types/@google-cloud/datastore/index.d.ts

您可以使用tsc --traceResolution快速查看tsc在搜索类型定义时正在考虑的文件/文件夹。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43508237

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档