Question

I am trying to use RxJs definitions in TypeScript application I am writing. I imported the defs and now I am trying to new up a subject.

this.stream = new Rx.Subject();

However when I try this I get the following error.

Error 6 The property 'Subject' does not exist on value of type '{ Internals: { inherits(child: Function, parent: Function): Function; addProperties(obj: Object, ...sourcces: Object[]): void; addRef(xs: Rx.IObservable, r: { getDisposable(): Rx._IDisposable; }): Rx.IObservable; }; Observer: { create(onNext: (value: any) => void, onError?: (exception: any) => void, onCompleted?: () => void): Rx.IObserver; fromNotifier(handler: (notification: Rx.INotification) => void): Rx.IObserver; }; }

When I look at the type defs for this I can see that the Subject class has been defined as an interface

export interface Subject {
    (): ISubject;

    create(observer: IObserver, observable: IObservable): ISubject;
}

Can someone tell me how I can create an instance of this Subject class as I cannot find a way to do it. Currently using TS 9.1.1. I thought the (): ISubject might do it but doesn't seem to have any impact.

I know some suggestions are to define a class instead but then I would need to implement all the methods from the base interfaces so this would be huge as it inherits from IObservable.

Was it helpful?

Solution

Using the latest version of the rx.js typing from Definitely Typed this works. Updating your definition is the way to go.

You can also try it on the TypeScript Playground - you'll need to paste in the code from Definitely Typed to test it out (long-links to the playground no longer seem to be allowed on here).

var x = new Rx.Subject();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top