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 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.
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).
$ and also $$ are valid variable names in javascript with no special meaning whatsoever and thus also have no special meaning 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.
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
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