最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Typescript basic assignment into the if statement after compilation - Stack Overflow

programmeradmin1浏览0评论

I am trying to start with Angular2 Quickstart and found a mistake (feature?) inside appponent.jsafter tsc (version 1.8.2) piled appponent.ts:

if (d = decorators[i])

I have not found where I was wrong into the quickstart sample configuration. Thanks for any help!

My code:

appponent.ts

import {Component} from 'angular2/core';


@Component({
    selector: 'my-app',
    templateUrl: '/templates/AppComponent.html'
})
export class AppComponent {
    public name: string = "My test app";
}

package.json

{
  "name": "iSandBox",
  "version": "1.0.0",
  "scripts": {
    "start": "concurrent \"npm run tsc:w\" \"npm run grunt\" \"npm run lite\" ",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "lite": "lite-server",
    "typings": "typings",
    "postinstall": "typings install",
    "grunt": "grunt"
  },
  "license": "ISC",
  "dependencies": {
    "angular2": "2.0.0-beta.7",
    "systemjs": "0.19.22",
    "es6-promise": "^3.0.2",
    "es6-shim": "^0.34.4",
    "reflect-metadata": "0.1.3",
    "rxjs": "5.0.0-beta.2",
    "zone.js": "0.5.15"
  },
  "devDependencies": {
    "concurrently": "^2.0.0",
    "grunt": "^1.0.0-rc1",
    "grunt-contrib-copy": "^0.8.2",
    "grunt-contrib-jshint": "~1.0.0",
    "grunt-contrib-nodeunit": "~0.4.1",
    "grunt-contrib-uglify": "~0.11.1",
    "gulp": "^3.9.1",
    "gulp-concat": "^2.6.0",
    "gulp-rename": "^1.2.2",
    "gulp-tslint": "^4.3.2",
    "gulp-uglify": "^1.5.3",
    "gulp-util": "^3.0.7",
    "lite-server": "^2.1.0",
    "typescript": "^1.7.5",
    "typings": "^0.6.8"
  },
  "ambientDependencies": {
    "es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#6697d6f7dadbf5773cb40ecda35a76027e0783b2"
  }
}

tsconfig.json

{
  "pilerOptions": {
    "target": "es5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules",
    "typings/main",
    "typings/main.d.ts"
  ]
}

appponent.js

System.register([ 'angular2/core' ],
    function(exports_1, context_1) {
        "use strict";
        var __moduleName = context_1 && context_1.id;
        var __decorate = (this && this.__decorate)
                || function(decorators, target, key, desc) {
                    var c = arguments.length, r = c < 3 ? target
                            : desc === null ? desc = Object
                                    .getOwnPropertyDescriptor(target, key) : desc, d;
                    if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
                        r = Reflect.decorate(decorators, target, key, desc);
                    else
                        for (var i = decorators.length - 1; i >= 0; i--)
/*=================HERE===>>>>*/  if (d = decorators[i])
                                r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
                    return
                            c > 3 && r && Object.defineProperty(target, key, r), r;
                };
        var __metadata = (this && this.__metadata)
                || function(k, v) {
                    if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
                        return Reflect.metadata(k, v);
                };
        var core_1;
        var AppComponent;
        return {
            setters : [ function(core_1_1) { core_1 = core_1_1; } ],
            execute : function() {
                AppComponent = (function() {
                    function AppComponent() {
                        this.name = "My test app";
                    }
                    AppComponent = __decorate(
                            [core_1.Component({
                                        selector : 'my-app',
                                        templateUrl : '/templates/AppComponent.html'
                                    }),
                            __metadata('design:paramtypes',[]) ], AppComponent);
                    return AppComponent;
                }());
                exports_1("AppComponent", AppComponent);
            }
        }
    });
// # sourceMappingURL=appponent.js.map

I am trying to start with Angular2 Quickstart and found a mistake (feature?) inside app.ponent.jsafter tsc (version 1.8.2) piled app.ponent.ts:

if (d = decorators[i])

I have not found where I was wrong into the quickstart sample configuration. Thanks for any help!

My code:

app.ponent.ts

import {Component} from 'angular2/core';


@Component({
    selector: 'my-app',
    templateUrl: '/templates/AppComponent.html'
})
export class AppComponent {
    public name: string = "My test app";
}

package.json

{
  "name": "iSandBox",
  "version": "1.0.0",
  "scripts": {
    "start": "concurrent \"npm run tsc:w\" \"npm run grunt\" \"npm run lite\" ",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "lite": "lite-server",
    "typings": "typings",
    "postinstall": "typings install",
    "grunt": "grunt"
  },
  "license": "ISC",
  "dependencies": {
    "angular2": "2.0.0-beta.7",
    "systemjs": "0.19.22",
    "es6-promise": "^3.0.2",
    "es6-shim": "^0.34.4",
    "reflect-metadata": "0.1.3",
    "rxjs": "5.0.0-beta.2",
    "zone.js": "0.5.15"
  },
  "devDependencies": {
    "concurrently": "^2.0.0",
    "grunt": "^1.0.0-rc1",
    "grunt-contrib-copy": "^0.8.2",
    "grunt-contrib-jshint": "~1.0.0",
    "grunt-contrib-nodeunit": "~0.4.1",
    "grunt-contrib-uglify": "~0.11.1",
    "gulp": "^3.9.1",
    "gulp-concat": "^2.6.0",
    "gulp-rename": "^1.2.2",
    "gulp-tslint": "^4.3.2",
    "gulp-uglify": "^1.5.3",
    "gulp-util": "^3.0.7",
    "lite-server": "^2.1.0",
    "typescript": "^1.7.5",
    "typings": "^0.6.8"
  },
  "ambientDependencies": {
    "es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#6697d6f7dadbf5773cb40ecda35a76027e0783b2"
  }
}

tsconfig.json

{
  "pilerOptions": {
    "target": "es5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules",
    "typings/main",
    "typings/main.d.ts"
  ]
}

app.ponent.js

System.register([ 'angular2/core' ],
    function(exports_1, context_1) {
        "use strict";
        var __moduleName = context_1 && context_1.id;
        var __decorate = (this && this.__decorate)
                || function(decorators, target, key, desc) {
                    var c = arguments.length, r = c < 3 ? target
                            : desc === null ? desc = Object
                                    .getOwnPropertyDescriptor(target, key) : desc, d;
                    if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
                        r = Reflect.decorate(decorators, target, key, desc);
                    else
                        for (var i = decorators.length - 1; i >= 0; i--)
/*=================HERE===>>>>*/  if (d = decorators[i])
                                r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
                    return
                            c > 3 && r && Object.defineProperty(target, key, r), r;
                };
        var __metadata = (this && this.__metadata)
                || function(k, v) {
                    if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
                        return Reflect.metadata(k, v);
                };
        var core_1;
        var AppComponent;
        return {
            setters : [ function(core_1_1) { core_1 = core_1_1; } ],
            execute : function() {
                AppComponent = (function() {
                    function AppComponent() {
                        this.name = "My test app";
                    }
                    AppComponent = __decorate(
                            [core_1.Component({
                                        selector : 'my-app',
                                        templateUrl : '/templates/AppComponent.html'
                                    }),
                            __metadata('design:paramtypes',[]) ], AppComponent);
                    return AppComponent;
                }());
                exports_1("AppComponent", AppComponent);
            }
        }
    });
// # sourceMappingURL=app.ponent.js.map
Share Improve this question asked Feb 28, 2016 at 7:42 MichaelMichael 1,4733 gold badges20 silver badges29 bronze badges 2
  • What's wrong with that? – elclanrs Commented Feb 28, 2016 at 7:43
  • What's wrong with assignment and parison? – Michael Commented Feb 28, 2016 at 7:45
Add a ment  | 

1 Answer 1

Reset to default 3

Assignment within a test isn't necessarily wrong. This:

if (d = decorators[i])

is the same as this:

d = decorators[i];
if (d)

Let's look at the whole if:

if (d = decorators[i])
    r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;

As you can see, the body of the if is then using d as a function, so this is just a guard on that, to prevent calling something that's falsy (and thus isn't a function).

It's just looping through decorators and, for non-falsy ones, calling them.

发布评论

评论列表(0)

  1. 暂无评论