TypeScript / Angular2: rxjs TS1138, TS1005, TS1128 errors

I follow the Angular 2 tutorial, and when I start gulp dev, I get these errors in the terminal. I am using Typescript 2.0.3:

node_modules/rxjs/operator/toPromise.d.ts(8,38): Error TS1138: Parameter declaration expected.
node_modules/rxjs/operator/toPromise.d.ts(8,42): Error TS1005: ';' expected.
node_modules/rxjs/operator/toPromise.d.ts(8,57): Error TS1005: '(' expected.
node_modules/rxjs/operator/toPromise.d.ts(8,58): Error TS1005: ';' expected.
node_modules/rxjs/operator/toPromise.d.ts(8,70): Error TS1005: '(' expected.
node_modules/rxjs/operator/toPromise.d.ts(9,38): Error TS1138: Parameter declaration expected.
node_modules/rxjs/operator/toPromise.d.ts(9,42): Error TS1005: ';' expected.
node_modules/rxjs/operator/toPromise.d.ts(9,57): Error TS1109: Expression expected.
node_modules/rxjs/operator/toPromise.d.ts(9,70): Error TS1005: ';' expected.
node_modules/rxjs/operator/toPromise.d.ts(9,86): Error TS1005: ';' expected.
node_modules/rxjs/operator/toPromise.d.ts(9,87): Error TS1128: Declaration or statement expected.
node_modules/rxjs/operator/toPromise.d.ts(9,99): Error TS1005: '(' expected.

And in the browser, the Angular application does not load. Can someone explain what is happening?

EDIT: these are lines 8 and 9 of node_modules/rxjs/operator/toPromise.d.ts

export declare function toPromise<T>(this: Observable<T>): Promise<T>;
export declare function toPromise<T>(this: Observable<T>, PromiseCtor: typeof Promise): Promise<T>;

And this is the only file that uses the method toPromise:

weather.service.ts

import {Injectable} from "@angular/core";
import {Http} from "@angular/http";
import "rxjs/add/operator/toPromise";

interface WeatherApiResponse {
    query: {
        count: number;
        created: string;
        lang: string;
        results: {
            channel: {
                item: {
                    condition: {
                        code: string;
                        temp: string
                    }
                }
            }
        }
    };
}

export interface WeatherInformation {
    city: string;
    code: number;
    temperature: number;
}

export interface City {
    name: string;
    imageSrc: string;
    woeId: string;
}

@Injectable()
export class WeatherService {
    cities = [
        {name: "Bogota", imageSrc: "img/bogota.jpg", woeId: "368148"}
    ];

    constructor(private http: Http) {}

    getWeather(woeId: string) {
        const url = this.generateWeatherUrl(woeId);
        return this.http.get(url).toPromise()
            .then(x => {
                const apiResponse = x.json() as WeatherApiResponse;
                const weather = apiResponse.query.results.channel.item.condition;
                return {
                    city: this.getCityName(woeId),
                    code: Number(weather.code),
                    temperature: Number(weather.temp)
                } as WeatherInformation;
            });
    }

    private generateWeatherUrl(woeId: string) {
        return `http://localhost:8001/api/weather/${woeId}`;
    }

    private getCityName(woeId: string) {
        const matches = this.cities.filter(x => x.woeId === woeId);
        return matches.length === 1 ? matches[0].name : undefined;
    }
}

This is the function that calls the method getWeather()inweather.service.ts

weather.component.ts

ngOnInit() {
    this.route.params.subscribe(params => {
        const woeId = params["woeId"];
        this.weatherService.getWeather(woeId)
            .then(x => this.weather = x);
    });
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": true,
    "noImplicitAny": true
  },
  "exclude": [
    "node_modules",
    "typings/main",
    "typings/main.d.ts"
  ]
}
+4
source share
2 answers

Packt Angular 2 Levi Botelho gulp , , (@ angular).

:

  "dependencies": {
    "@angular/common": "~2.1.1",
    "@angular/compiler": "~2.1.1",
    "@angular/core": "~2.1.1",
    "@angular/forms": "~2.1.1",
    "@angular/http": "~2.1.1",
    "@angular/platform-browser": "~2.1.1",
    "@angular/platform-browser-dynamic": "~2.1.1",
    "@angular/router": "~3.1.1",
    "@angular/upgrade": "~2.1.1",
    "angular-in-memory-web-api": "~0.1.13",
    "core-js": "^2.4.1",
    "reflect-metadata": "^0.1.8",
    "systemjs": "0.19.39",
    "zone.js": "^0.6.25",
    "compression": "^1.6.2",
    "es6-shim": "^0.35.1",
    "express": "^4.14.0",
    "rxjs": "5.0.0-beta.12"
  } 

gulp.js, , , , ...

const argv = require("yargs").argv;
const browserify = require("browserify");
const browserSync = require("browser-sync").create();
const buffer = require("vinyl-buffer");
const cache = require("gulp-cached");
const concat = require("gulp-concat");
const exec = require('child_process').exec;
const gulp = require("gulp");
const gulpIf = require("gulp-if");
const htmlmin = require("gulp-htmlmin");
const inject = require("gulp-inject");
const sass = require("gulp-sass");
const source = require("vinyl-source-stream");
const sourcemaps = require("gulp-sourcemaps");
const tsify = require("tsify");
const tslint = require("gulp-tslint");
const uglify = require("gulp-uglify");
const watchify = require("watchify");

const rootBuildPath = "./dist/";
const cssBundleName = "site.css";
const cssBundleBuildPath = rootBuildPath + "/css/" + cssBundleName;
const cssSource = "./scss/site.scss";
const cssBuildPath = rootBuildPath + "css";
const fontSource = "./bower_components/weather-icons/font/**";
const fontBuildPath = rootBuildPath + "font";
const htmlSource = "./index.html";
const imgSource = "./img/*.jpg";
const imgBuildPath = "./dist/img";
const jsBundleName = "bundle.js";
const jsBundleBuildDirectory = rootBuildPath + "app";
const jsBundleBuildPath = jsBundleBuildDirectory + "/bundle.js";
const jsSourceDirectory = "./app";
const jsEntryPoint = jsSourceDirectory + "/main.ts";
const jsSource = jsSourceDirectory + "/**/*.ts";
const libSource = [
    "node_modules/es6-shim/es6-shim.min.js",
    "node_modules/es6-shim/es6-shim.map",
    "node_modules/zone.js/dist/zone.min.js",
    "node_modules/reflect-metadata/Reflect.js",
    "node_modules/reflect-metadata/Reflect.js.map",
];
const libBuildPath = rootBuildPath + "/lib";
const serverSource = "./server.js";
const templatesSource = "./app/**/*.html";
const templatesBuildPath = rootBuildPath + "app";
const typings = "./typings/index.d.ts";

function logError(err) {
    console.error(err.message);
    this.emit("end");
}

function shouldMinify() {
    return argv.release;
}

gulp.task("lint", function () {
    gulp.src(jsSource)
        .pipe(tslint({
            formatter: "verbose"
        }))
        .pipe(tslint.report())
        .on("error", logError);
});

var browserifyOptions = {
    debug: !shouldMinify(),
    entries: [jsEntryPoint, typings],
    plugin: [tsify]
};

if (argv.watch) {
    browserifyOptions.cache = {};
    browserifyOptions.packageCache = {};
    browserifyOptions.plugin.push(watchify);
}

var browserifyInstance = browserify(browserifyOptions);

gulp.task("js", ["lint"], function () {
    return browserifyInstance
        .bundle()
        .on("error", logError)
        .pipe(source(jsBundleName))
        .pipe(buffer())
        .pipe(sourcemaps.init({loadMaps: true}))
        .pipe(gulpIf(shouldMinify(), uglify().on("error", logError)))
        .pipe(sourcemaps.write("./"))
        .pipe(gulp.dest(jsBundleBuildDirectory));
});

gulp.task("css", function () {
    return gulp.src(cssSource)
        .pipe(concat(cssBundleName))
        .pipe(sourcemaps.init())
        .pipe(sass({outputStyle: shouldMinify() ? "compressed" : "nested"}))
        .pipe(sourcemaps.write("."))
        .pipe(gulp.dest(cssBuildPath))
        .pipe(browserSync.stream());
});

gulp.task("font", function () {
    return gulp.src(fontSource)
        .pipe(gulp.dest(fontBuildPath));
})

gulp.task("img", function () {
    return gulp.src(imgSource)
        .pipe(gulp.dest(imgBuildPath));
});

gulp.task("lib", function () {
    return gulp.src(libSource)
        .pipe(gulp.dest(libBuildPath));
});

gulp.task("templates", function () {
    return gulp.src(templatesSource)
        .pipe(gulp.dest(templatesBuildPath));
});

gulp.task("html", ["js", "css"], function () {
    return gulp.src(htmlSource)
        .pipe(inject(gulp.src([jsBundleBuildPath, cssBundleBuildPath], {read: false}), {ignorePath: "dist"}))
        .pipe(gulpIf(shouldMinify(), htmlmin({collapseWhitespace: true})))
        .pipe(gulp.dest(rootBuildPath));
});

gulp.task("server", function () {
    return gulp.src(serverSource)
        .pipe(gulp.dest(rootBuildPath));
})

gulp.task("default", ["font", "html", "img", "lib", "templates", "server"]);

gulp.task("html-watch", ["html"], () => browserSync.reload());
gulp.task("js-watch", ["js"], () => browserSync.reload());
gulp.task("templates-watch", ["templates"], () => browserSync.reload());

gulp.task("dev", ["default"], function () {
    exec("node dist/server", function (err, stdout, stderr) {
        console.log(stdout);
        console.error(stderr);
    });

    gulp.watch(htmlSource, ["html-watch"]);
    gulp.watch(jsSource, ["js-watch"]);
    gulp.watch(templatesSource, ["templates-watch"]);
    gulp.watch(cssSource, ["css"]);

    browserSync.init({
        port: 8001,
        proxy: "http://localhost:3011"
    });
});

, , / .

+1

, .ts .js, , Visual Studio Code . , , 2.

2. tasks.json

. ⇧⌘P " ", , ....

TypeScript - tsconfig.json. tasks.json .vscode.

tasks.json :

{
  // See http://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "0.1.0",
  "command": "tsc",
  "isShellCommand": true,
  "args": ["-p", "."],
  "showOutput": "silent",
  "problemMatcher": "$tsc"
}

tsc : TypeScript JavaScript. , , : tsc -p .

0

Source: https://habr.com/ru/post/1658388/


All Articles