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

For a type variable, Observable<T>

I tried to assign Observable<S>

where S

is part T

and am surprised that there is no assignment error at compile time.

I would like to take the brains to understand the reason for this behavior.

// 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});

      

+3


source to share


1 answer


It is not actually related to RxJS. This is a TypeScript issue and has been fixed in the future 2.4

. This is why it works when you install typescript@next

.



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

0


source







All Articles