javascript - 在 typescript 内部模块中使用外部 typescript 库

标签 javascript typescript

假设您需要在一个内部模块中使用 typescript /节点库,该模块分布在多个 .ts 文件中。

ApiRepositoryHelper.ts

import * as requestPromise from "request-promise"; 
module ApiHelper {
   class ApiRepositoryHelper extends DataRepositoryHelper {}
}

DataRepositoryHelper.ts

module ApiHelper {
   class DataRepositoryHelper {}
}

只要我添加一行: import * as requestPromise from "request-promise";DataRepositoryHelper 变得不可访问。解决方案是什么?

观察:

如果我错了请纠正我:

  1. Typescript 内部模块可以拆分成多个文件。
  2. Typescript 外部模块不能。
  3. 如果您在同一文件中的内部模块(称为“A”)范围之外添加对外部模块(例如 jQuery)的引用,则可以在 A 中使用 JQuery。但是您的内部模块“A”在项目的其余部分变得不可访问。
  4. 如果你有一个外部模块,你可以引用其他外部模块。
  5. 您不能在内部模块范围内引用外部模块,您将得到“命名空间中的导入声明无法引用模块。”

我读过:Correct way to reference modules from other modules in Typescript .

我还读过:

Internal Module does not work if it has an import statement before it ,但我不同意:

you can organise your code really well without internal modules

只是因为,

module ApiHelper {
 class myClass {}
}

比,更整洁

import {Model} from "../../../lib/interfaces/model/Model";
import {List} from "../../../lib/classes/helper/List";
import {Serializable} from "../../interfaces/model/Serializable";
import {DataRepository} from "../../../lib/interfaces/data/DataRepository";
import {ModelFactory} from "../../interfaces/model/modelFactory";
import {DefaultApiParser} from "./DefaultApiParser";
import {ApiItemParser} from "./ApiParser";
//Insert two hundred other imports here...

export class MyClass{}

此外,在我看来,使用模块更优越,因为您没有使用文件系统来引用您的类(这是推荐的方式?!)- 只是将它们分开而不关心它们在哪里。

最佳答案

相信我们! (那些尝试过并烧毁自己的人),modules/namespaces 最好尽可能避免。

使用它们可能会让你解决很多无趣的问题,比如

“整洁”也值得商榷

  • 如果这能让你感觉更好的话,Java 开发人员在 Typescript 出现之前的很多年里已经在他们的文件顶部进行了许多导入,我希望他们的预期生命周期不比 Javascript 开发人员短(作为一个他们也是)。更严重的是,Webstorm (12 EAP) 等一些 IDE 会自动为您处理导入,甚至会在编辑器中折叠它们,以免它们“污染”您的代码 View 。

  • 此外,从代码设计的 Angular 来看,您正在做的是 import * 而不是导入您只需要的东西是值得商榷的。

总的来说,外部模块的隔离效果更好。

现在假设您的代码分为两部分:API 和实现,并且您希望在实现类中一次导入所有 API 定义以避免多个导入语句。

可以在 API“部分”中创建一个重新导出所有内容的 api.ts 文件,即

export * from 'interfaces/model/Model' 
export * from 'interfaces/model/Interfaces'
export * from 'interfaces/model/Serializables'
...

然后在实现类中简单地在命名空间下导入单个 api.ts 文件,例如;

import * as api from '../api/api.ts'

然后可以访问所有 API 定义的导出为 api.Listapi.MyModel 等...

但是,既然您已经做到了,为什么不一路走下去,为您的 API 定义创建一个外部模块呢? :)

创建库

不幸的是,我暂时找不到这方面的权威资源。

创建库的 TL;DR 是:

  • 创建一个 index.ts 来重新导出您想要在库中公开的所有定义
  • 使用--declaration 标志编译库文件以自动生成类型
  • package.json 中添加一个 typings 条目指向生成的 index.d.ts 文件

使用库:

  • 像任何其他库一样将其放入 node_modules 文件夹
  • 只需 import * as mylib from 'mylib':它将自动导入所有导出的定义类型

有一个简短的提及here

你也可以看看我写的LibraryUsage段落there

如果我找到更好的资源,我会更新这个答案

关于javascript - 在 typescript 内部模块中使用外部 typescript 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35582275/

相关文章:

javascript - 火狐插件开发 : Detecting non-compatible addons?

javascript - 如何使用 Grunt.js 将消息添加到输出文件?

javascript - 需要帮助声明具有不同名称的数组,JavaScript

angular - 如何使用 Angular2 将数据发送到 ASP.NET Controller

Angular:Observable 在使用管道和选项卡时改变行为

javascript - html input type=file -- 从 iCloud 选择一个文件

选择地区时,PHP 从数据库中填充数据

angular - RXJS 'never' 主题为泛型

css - 使用 Angular 4 检测 html 元素的碰撞或触摸

javascript - .Map函数差异javascript typescript