具有所有可选成员的接口的TypeScript值声明被视为<any>

时间:2016-07-20 17:39:36

标签: angularjs typescript typescript1.8

以下代码是有效的TypeScript

interface AllMembersOptional {
    first?: string;
    second?: number;
}

let a: AllMembersOptional = 7;
let b: AllMembersOptional = () => 7;

并且因为Angular的IComponentOptions接口被定义为

interface IComponentOptions {
    controller?: string | Function | (string | Function)[] | IComponentController;
    controllerAs?: string;
    template?: string | Function | (string | Function)[];
    templateUrl?: string | Function | (string | Function)[];
    bindings?: {[binding: string]: string};
    transclude?: boolean | string | {[slot: string]: string};
    require?: {[controller: string]: string};
}

然后以下是有效的TypeScript:

angular.module('test').component('custom', function () {
    return {
        controller: 'testCtrl',
        template: '<div></div>,
        //...
    };
});

angular.module('test').component('custom', 7);

angular.module('test').component('custom', "hello");

angular.module('test').component('custom', new Date());

这些代码在运行时实际上都不起作用,但TypeScript编译没有错误,并且正在按预期工作

TypeScript编译器是否应该生成“不良/不兼容”类型&#39;以上例子的错误? 或者是否应该禁止空接口(例如通过编译器开关)? 或者什么都不做,只是接受它是如何运作的?

1 个答案:

答案 0 :(得分:1)

这已记录为issue 7485。在撰写本文时,尚未解决。