I have a problem when I try to start my angular 6 project. I've got this error :
"TSError: ⨯ Unable to compile TypeScript: git.version.ts(34,29): error TS2339: Property 'combineLatest' does not exist on type 'typeof Observable'."
It worked well with Angular 5 but I updated RxJs (from 5 to 6) and now it doesn't work.
import { readFileSync, writeFileSync } from 'fs';
import { join } from 'path';
import { Observable, combineLatest } from 'rxjs';
let exec = require('child_process').exec;
let tag = new Observable<string>(s => {
exec('git describe --tags $(git rev-list --tags --max-count=1)',
function (error: Error, stdout: Buffer, stderr: Buffer) {
if (error !== null) {
console.log('git error: ' + error + stderr);
}
s.next(stdout.toString().trim());
s.complete();
});
});
let revision = new Observable<string>(s => {
exec('git rev-parse --short HEAD',
function (error: Error, stdout: Buffer, stderr: Buffer) {
if (error !== null) {
console.log('git error: ' + error + stderr);
}
s.next(stdout.toString().trim());
s.complete();
});
});
Observable.combineLatest(tag, revision).subscribe(([tag, revision]) => {
console.log(`version: '${tag}', revision: '${revision}'`);
const content = '// this file is automatically generated\n' +
`export const version = {version: '${tag}', revision: '${revision}'};`;
writeFileSync(
'src/environments/version.ts',
content,
{encoding: 'utf8'}
);
});
The command I launch : "ts-node git.version.ts" is to get the commit and the tag version I work with.
Thank you very much !
UPDATE :
combineLatest(tag, revision).subscribe(([tag, revision]) => {
console.log(`version: '${tag}', revision: '${revision}'`);
const content = '// this file is automatically generated\n' +
`export const version = {version: '${tag}', revision: '${revision}'};`;
writeFileSync(
'src/environments/version.ts',
content,
{encoding: 'utf8'}
);
});
I haven't any error about CombineLatest but I have an error from ts-node :
function (exports, require, module, __filename, __dirname) { import { writeFileSync } from 'fs';
^^^^^^
SyntaxError: Unexpected token import
Any idea ?
UPDATE 2 : To fix this error :
"config": "ts-node -O '{\"module\": \"commonjs\"}' git.version.ts",
In package.json
Angular6 comes with rxjs6. This is the proper format for combineLatest:
import before class declaration from the new 'rxjs' location:
// RxJS v6+
import { timer, combineLatest } from 'rxjs';
example of use inside a function:
//timerOne emits first value at 1s, then once every 4s
const timerOne = timer(1000, 4000);
//timerTwo emits first value at 2s, then once every 4s
const timerTwo = timer(2000, 4000);
//timerThree emits first value at 3s, then once every 4s
const timerThree = timer(3000, 4000);
//when one timer emits, emit the latest values from each timer as an array
const combined = combineLatest(timerOne, timerTwo, timerThree);
const subscribe = combined.subscribe(
([timerValOne, timerValTwo, timerValThree]) => {
/*
Example:
timerOne first tick: 'Timer One Latest: 1, Timer Two Latest:0, Timer Three Latest: 0
timerTwo first tick: 'Timer One Latest: 1, Timer Two Latest:1, Timer Three Latest: 0
timerThree first tick: 'Timer One Latest: 1, Timer Two Latest:1, Timer Three Latest: 1
*/
console.log(
`Timer One Latest: ${timerValOne},
Timer Two Latest: ${timerValTwo},
Timer Three Latest: ${timerValThree}`
);
}
);
Of course, in your case you'd replace the timers with your tag, revision
items.
Code excerpt from:
https://www.learnrxjs.io/operators/combination/combinelatest.html
which contains more examples
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With