Reputation: 3661
Using nodejs and typescript to review another answer to a different question, I get this exception:
TypeError: interval$.zip is not a function
My code (rxjs2.ts):
{
var Rx = require('rxjs');
const interval$ = Rx.interval(1000);
const items$ = Rx.from([1,2,3]);
const itemsOverTime$ = interval$.zip(items$).repeat();
itemsOverTime$.subscribe(([time, val]) => {
console.log(val);
// 1
// 2
// 3
// 1
// 2
// 3
});
}
Running this from VSCode console, like: node rxjs2.ts
My package-lock.json
{
"requires": true,
"lockfileVersion": 1,
"dependencies": {
"@types/node": {
"version": "10.12.2",
"resolved": "https://registry.npmjs.org/@types/node/-/node-10.12.2.tgz",
"integrity": "sha512-53ElVDSnZeFUUFIYzI8WLQ25IhWzb6vbddNp8UHlXQyU0ET2RhV5zg0NfubzU7iNMh5bBXb0htCzfvrSVNgzaQ=="
},
"rxjs": {
"version": "6.3.3",
"resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.3.3.tgz",
"integrity": "sha512-JTWmoY9tWCs7zvIk/CvRjhjGaOd+OVBM987mxFo+OW66cGpdKjZcpmc74ES1sB//7Kl/PAe8+wEakuhG4pcgOw==",
"requires": {
"tslib": "1.9.3"
}
},
"tslib": {
"version": "1.9.3",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-1.9.3.tgz",
"integrity": "sha512-4krF8scpejhaOgqzBEcGM7yDIEfi0/8+8zDRZhNZZ2kjmHJ4hv3zCbQWxoJGz1iw5U0Jl0nma13xzHXcncMavQ=="
}
}
}
Upvotes: 2
Views: 4756
Reputation: 449
Your imports are wrong and also the way you access the imports.
This should work:
import { interval, from, zip } from 'rxjs';
import { repeat } from 'rxjs/operators';
const interval$ = interval(1000);
const items$ = from([1, 2, 3]);
const itemsOverTime$ = zip(interval$, items$).pipe(repeat());
itemsOverTime$.subscribe(([time, val]) => {
console.log(val);
// 1
// 2
// 3
// 1
// 2
// 3
});
UPDATE: Here is a solution for CommonJS imports. You need to install rxjs-compat with npm, if you prefer to use the old syntax and imports. Look at this documentation Rxjs Doc.
const Rx = require('rxjs/Rx');
const repeat = require('rxjs/operator/repeat');
const interval$ = Rx.Observable.interval(1000);
const items$ = Rx.Observable.from([1, 2, 3]);
const itemsOverTime$ = Rx.Observable.zip(interval$, items$).repeat();
itemsOverTime$.subscribe(([time, val]) => {
console.log(val);
// 1
// 2
// 3
// 1
// 2
// 3
});
UPDATE 2:
For CommonJS and the new version of RxJs you need to do it like this:
const Rx = require('rxjs');
const RxOp = require('rxjs/operators');
const interval$ = Rx.interval(1000);
const items$ = Rx.from([1, 2, 3]);
const itemsOverTime$ = Rx.zip(interval$, items$).pipe(RxOp.repeat());
itemsOverTime$.subscribe(([time, val]) => {
console.log(val);
// 1
// 2
// 3
// 1
// 2
// 3
});
Upvotes: 6