Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type Declaration Order in TypeScript

Tags:

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.

like image 848
Kaveh Shahbazian Avatar asked Jan 15 '16 12:01

Kaveh Shahbazian


People also ask

What is type declaration in TypeScript?

TypeScript includes declaration files for all of the standardized built-in APIs available in JavaScript runtimes. This includes things like methods and properties of built-in types like string or function , top-level names like Math and Object , and their associated types.

How do I write a TypeScript declaration?

We can write our own TypeScript Declaration files or have them produced from the compilation of TypeScript files ( . ts ) by setting declaration compiler-option to true in the tsconfig. json file (or using --declaration flag with the tsc command).

What is $$ in TypeScript?

$ and also $$ are valid variable names in javascript with no special meaning whatsoever and thus also have no special meaning in typescript.

Which is the correct method to declare a variable in TypeScript?

The type syntax for declaring a variable in TypeScript is to include a colon (:) after the variable name, followed by its type. Just as in JavaScript, we use the var keyword to declare a variable.


2 Answers

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.

like image 193
vasa_c Avatar answered Oct 16 '22 04:10

vasa_c


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

like image 41
Amid Avatar answered Oct 16 '22 04:10

Amid