TypeScript对类型声明顺序敏感吗?
更改类型的顺序会导致错误,这在 Angular 2 (beta.0) 内部会出现一个错误。据我所知,Angular 2 是使用 TypeScript 实现的(这就是为什么这个错误看起来如此奇怪和无关紧要):
angular2-polyfills.js:138 错误:无法读取未定义的 'prototype' 属性(…)
假设我们有一个名为 t1.ts
的文件:
export class AuthResponse extends JsonResponse { }
export class JsonResponse {
public code: ResultCode;
}
export enum ResultCode { }
启动应用程序时,我们在客户端看到了上述错误。但是,如果我们反转此文件中的声明顺序,该错误将消失(仅供记录,目前我正在继续前进,并牢记这一点&它有效)。
要重现此错误,我们需要另外五个文件:
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, []);
还有一个稍微大一点的文件index.html
,但本质上与angular网站上的教程中的index.html
结构相同。