打字稿未导入角装饰器“类”

时间:2018-10-21 14:04:31

标签: angular typescript typescript-decorator

我正在开发一个有角度的应用程序,并与仅在装饰器中使用的类作斗争。该文件不是由编译的源导入的。它很可能在Typescript编译器中进行了优化,因为它仅在装饰器中使用。但是,即使从未声明过“导入的”变量,它也仍在元数据中使用(请注意,第46行JS中的“ typeof firstclass_model_1.FirstClass”未定义firstclass_model_1)。

这是Typescript还是Angular中的错误?

我可以通过使用打字稿源中的FirstClass解决此问题。

打字稿来源:

public class MyGraph : PXGraph<MyGraph>
{
    // Some other views...

    /// <summary>
    /// View for tab 1
    /// </summary>
    public PXSelect<MyTableTypeA> TypeA;
    /// <summary>
    /// View for tab 2
    /// </summary>
    public PXSelect<MyTableTypeB> TypeB;
    /// <summary>
    /// View for tab 3
    /// </summary>
    public PXSelect<MyTableTypeC> TypeC;

    // When using events and cache attached you use the DAC name. Ex: 
    //  protected virtual void MyTableTypeB_RowDeleting(PXCache cache, RowDeletingEvents e) { }
}

完整来源:https://pastebin.com/aNrShiVj

已翻译的JS:

import { Component, OnInit, Input } from "@angular/core";
import { FirstClass } from "~/shared/firstclass.model";
import { SecondClass } from "~/shared/secondclass.model";
import { DataService } from "~/shared/data.service";

@Component({
    selector: "test-component",
    moduleId: module.id,
    templateUrl: "./test.component.html",
    styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {

    tickettypes: TicketType[];

    @Input("first")
    public get first() {
        return this._first;
    };
    public set first(value: FirstClass) {
        this._first = value;
    }
    private _first: FirstClass;

    @Input("second")
    public get second() {
        return this._second;
    };
    public set second(value: SecondClass) {
        this._second = value;
    }
    private _second: SecondClass;

    constructor(
        private dataService: DataService
    ) {
        // Use the component constructor to inject providers.
    }

    ngOnInit(): void {

    }

    itemTap(tickettype: TicketType) {
        let item = new SecondClass( {
            foo: "bar"
        });
        this.dataService.addItem(item);
    }

}

https://pastebin.com/2KeE0c9p

1 个答案:

答案 0 :(得分:0)

以下简化示例:

// foo.ts
export class Foo {
    x: string;
}

// code.ts
import { Foo } from "./foo";

function MyDecorator(target: Object, key: PropertyKey) {}

class MyClass {
    @MyDecorator
    get myField() {
        return this._myField;
    }
    set myField(value: Foo) {
        this._myField = value;
    }
    private _myField: Foo;
}

使用TypeScript 2.9.2为我重现了问题,但是没有使用TypeScript 3.1.3。请尝试升级TypeScript。 (如果Angular阻止了您的TypeScript版本,那您似乎很幸运:Angular 7刚刚发布,支持TypeScript 3.1。)