【问题标题】:Importing List type in Typescript在 Typescript 中导入列表类型
【发布时间】:2017-11-29 12:28:12
【问题描述】:

我是第一次学习 angular2+ 和 Typescript,我遇到了来自 website 的代码示例的问题。

他的一个代码示例使用了List 类型,但我不知道作者从哪里导入了这种类型。我尝试安装和导入typescript-collections,但它没有List 的实现。

如何安装和导入包含List 类型的模块?

更新:

这是使用List的代码的sn-p

@Injectable()
export class TodoStore {
    private _todos: BehaviorSubject<List<Todo>> = new BehaviorSubject(List([]));

    public readonly todos: Observable<List<Todo>> = this._todos.asObservable();

    constructor(private todoBackendService: TodoBackendService) {
        this.loadInitialData();
    }
    ...
}

【问题讨论】:

  • 如果你给我们你的代码 sn-p 会有很大帮助,这样我们就可以得到一些上下文......
  • 你想从List 那里得到什么Array 没有?
  • 他在这里导入列表:import {List} from 'immutable';
  • 如果您来自 C# 背景,那么我理解您为什么要使用 List 但是 javascript (typescript) Array 是一个非常相似的实现。

标签: angular typescript


【解决方案1】:

有问题的示例使用从Immutable JS 库导入的列表。您可以使用您选择的包管理器安装该库。

例如,使用 NPM

$ npm install immutable --save

使用 JSPM

$ jspm install immutable

用纱线

$ yarn add immutable

Immutable JS 带有自己的类型声明,因此您无需添加任何其他内容。

也就是说,现在考虑坚持使用简单的数组,直到您发现自己需要更复杂的东西。

【讨论】:

  • 太棒了。这很有效,很高兴知道。我将重构代码以改用 Array。
猜你喜欢
  • 1970-01-01
  • 2019-08-11
  • 1970-01-01
  • 1970-01-01
  • 2019-06-25
  • 2016-07-17
  • 2016-10-21
  • 2019-04-30
  • 2020-12-14
相关资源
最近更新 更多