【问题标题】:[Typescript][RxJS] Why is there no type error when returning an observable of partial type?[Typescript][RxJS] 为什么返回部分类型的 observable 时没有类型错误?
【发布时间】:2017-04-27 22:59:33
【问题描述】:

对于Observable<T> 类型的变量,我尝试分配一个Observable<S>,其中ST 的一部分,但令人惊讶的是编译期间没有分配错误。

很想开动脑筋来了解这种行为的原因。

// RxJS 5.3.0
// TS 2.2.2

interface Super {
  a: number;
  b: number;
}

interface Subset {
  a: number;
}

type Maybe<T> = T | undefined;

// Both of the following produce errors
const super1: Super = {a: 1} as Subset;
const superMaybe: Maybe<Super> = { a: 1 } as Maybe<Subset>;

// But this doesn't error
const superObservable: Rx.Observable<Super> = Rx.Observable.of<Subset>({a: 1});

【问题讨论】:

  • 有趣的是,如果你使用 typescript@next,它会被标记为错误。

标签: typescript rxjs rxjs5 typescript2.0


【解决方案1】:

这实际上与 RxJS 无关。这是一个 TypeScript 问题,已在未来的 2.4 版本中修复。这就是安装 typescript@next 时它起作用的原因。

见:https://github.com/Microsoft/TypeScript/issues/14770

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-21
    • 2020-07-29
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 2020-11-12
    • 2020-07-29
    相关资源
    最近更新 更多