简体   繁体   中英

Type Declaration Order in TypeScript

Is TypeScript sensitive to the order of type declarations?

Changing the order of type causes an error inside Angular 2 (beta.0) which (AFAIK) is implemented using TypeScript itself (that's why this error seems so strange & irrelevant to me):

angular2-polyfills.js:138 Error: Cannot read property 'prototype' of undefined(…)

Assume we have a file t1.ts :

export class AuthResponse extends JsonResponse { }
export class JsonResponse {
    public code: ResultCode;
}
export enum ResultCode { }

When starting the app, we see the mentioned error, at client side. But if we reverse the order of declarations in this file, the error goes away (Just for the record, currently I'm moving forward, keeping this in mind & it works).

To reproduce this error we need five more files:

t2.ts :

import {AuthResponse, JsonResponse, ResultCode} from './t1'; // this order?

export class DummyAction {
    doSomething() {
        console.log('test, starting ...');

        var v = new AuthResponse();
        return v;
    }
}

app.component.ts :

import {Component, OnInit} from 'angular2/core';
import {DummyAction} from './components/t2';            

@Component({      
    selector: 'root-app',
    templateUrl: '/app/templates/app.html',
})
export class AppComponent implements OnInit {
    constructor() {
        var tester = new DummyAction();
        // tester.runTest();
    }

    ngOnInit() { }
}

app.html :

<h1>TEST</h1>

boot.ts :

import {bootstrap} from 'angular2/platform/browser';
import {AppComponent} from './app.component';

bootstrap(AppComponent, []);

And index.html which is a bit larger but essentially, has the structure of index.html from tutorial on angular website.

TypeScript itself not sensitive, but this compiled to JS.

class A extends B {}
class B {}

In JS:

var A = (function (_super) { /* ... */ })(B);
var B = (function () { /* ... */  })();

B is undefined on line 1.

I have casted my vote for the vasa_c answer as it is the correct one. Below I would like to provide some more information on that matter so it will be easier for OP to understand the issue.

If we will take your sample code:

export class AuthResponse extends JsonResponse { }
export class JsonResponse {
    public code: ResultCode;
}
export enum ResultCode { }

And compile it - this is what the end result will be like:

var AuthResponse = (function (_super) {
    __extends(AuthResponse, _super);
    function AuthResponse() {
        _super.apply(this, arguments);
    }
    return AuthResponse;
})(JsonResponse);
exports.AuthResponse = AuthResponse;
var JsonResponse = (function () {
    function JsonResponse() {
    }
    return JsonResponse;
})();
exports.JsonResponse = JsonResponse;
(function (ResultCode) {
})(exports.ResultCode || (exports.ResultCode = {}));
var ResultCode = exports.ResultCode;

Note that the resulting code is not just function definitions. It is functions and variables. That make all the difference. Because you have both declarations and expressions. More on this matter and why with expressions in js order does matter you can read in this excellent post: JavaScript function declaration and evaluation order

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM