【问题标题】:What does it mean to "pass" an interface to an RxJS Store within angle braces?在尖括号中将接口“传递”到 RxJS Store 是什么意思?
【发布时间】:2019-09-24 09:13:38
【问题描述】:

使用 NgRx 和 typescript,我一直在类构造函数中看到这种语法:

import { Store, select } from '@ngrx/store'
class MyClass {
  constructor(private store: Store<AppState>) {
    this.count$ = store.pipe(select('users'));
  }
}

这个尖括号语法是做什么的? Store&lt;AppState&gt;

【问题讨论】:

  • 表示private store是一个AppStates的Store。括号是许多语言中子类型的常见指示符。

标签: javascript angular typescript ecmascript-6 rxjs


【解决方案1】:

这是Generic type 的示例。可能最简单的泛型示例是数组。您可以拥有 of 的数组,例如数字数组或字符串数​​组。使用泛型语法(数组确实有另一种可以使用的语法),这看起来像:

const myNumberArray: Array<number> = [1, 2];
const myStringArray: Array<string> = ['one', 'two'];

Store 也是一个泛型。您可以拥有一个 的存储,在这种情况下,它是一个 AppState 的存储。

【讨论】:

  • 所以它将我的store 的形状断言到Store 类?
  • 它具体说明了它期望的商店类型。它想要一个 AppState 的 Store,而不是任何其他东西的 Store。
  • 这与我所说的一致,还是更正..?
  • 取决于您所说的“断言”是什么意思。如果它说private store: number[],你会认为它是一个断言吗?如果是,那么我同意你的看法。但是“类型断言”在打字稿中还有另一个含义,其中打字稿无法确定类型是独立的,因此您要坚持打字稿将未知变量视为您断言的类型。这不是在这里发生的,所以如果这就是你的意思,那么我正在纠正你。
  • 我的意思是,“它将我的store 的形状传达给Store 类”
猜你喜欢
  • 2011-09-30
  • 2021-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-15
  • 2023-03-15
  • 1970-01-01
  • 2022-06-19
相关资源
最近更新 更多