TS如何知道引用const而不是接口

时间:2017-01-23 07:49:19

标签: typescript

我在metadata.ts中有以下内容:

export interface InjectableDecorator {
    (): any;
    new (): Injectable;
}

export interface Injectable {
}

export declare const Injectable: InjectableDecorator;

然后,我将Injectable导入到模块main.ts中,然后像这样使用它:

import {Injectable} from "@angular/core";
@Injectable()
export class ComboService {}

我有两个问题:

  1. TS编译器如何知道Injectable引用const Injectable而不是export interface Injectable
  2. 如果new @Injectable()允许使用error TS1109: Expression expected.执行功能,InjectableDecorator为什么会报告错误new

1 个答案:

答案 0 :(得分:3)

  1. 接口Injectable和var(本例中为const)Injectable实际上是两个不同的实体,适用于不同的空间。您也可以说let number:number = 4;当您将Injectable引用为值而不是类型时,TS引用const。

  2. @语法特定于将装饰器应用于目标。一个新的表达式期望......好吧......一个表达式。删除@并致电new Injectable(),它应该可以正常工作。