[Typescript][RxJS] 为什么返回部分类型的可观察对象时没有类型错误?

[Typescript][RxJS] Why is there no type error when returning an observable of partial type?

对于 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});

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

时它起作用的原因

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