Angularjs 包装类型以确保在Angular 2模板中检查运行时类型

Angularjs 包装类型以确保在Angular 2模板中检查运行时类型,angularjs,typescript,Angularjs,Typescript,一个已经花费了我几个小时的大问题是,Angular 2模板内部没有类型检查。这一点已在本节中讨论过。(但提供的唯一解决方案是AOT编译) 问题: @Component({ template: '<button (click)="foo='haha'"'><div>{{foo}}</div>' }) export class Bar { foo: number = 3; } @组件({ 模板:' function checkTypes(shou

一个已经花费了我几个小时的大问题是,Angular 2模板内部没有类型检查。这一点已在本节中讨论过。(但提供的唯一解决方案是AOT编译)

问题:

@Component({
    template: '<button (click)="foo='haha'"'><div>{{foo}}</div>'
})
export class Bar {
    foo: number = 3;
}
@组件({
模板:'
function checkTypes(shouldBe: any, value: any) {
    if(typeof shouldBe !== typeof value)
        throw "Typemissmatch. Expected a " + typeof shouldBe + ", but the provided variable '" + value + "' is of type " + typeof value + ".";
}

abstract class Typesafe<T> {
    private _value: T;

    constructor(start: T) {
        this._value = start;
    }

    get value(): T {
        return this._value;
    }

    set value(value: T) {
        checkTypes(this._value, value)
        this._value = value;
    }
}

export class TInt extends Typesafe<number> {};