Typescript: 类型“any”不是构造函数类型

创建于 2015-09-01  ·  3评论  ·  资料来源: microsoft/TypeScript

class Thing extends ((function () {
    return null;
})()) {
    doThing() {
    }
}

extends null是一种非常迂回的方式,但类型不检查,这很尴尬。 您必须使用像StringConstructor这样的虚拟 ctor 类型来注释 IIFE 以满足检查器。

By Design

最有用的评论

这是一个有趣的案例,在 TypeScript 2.0 中,我声明了一个模块 swagger-client ,如: declare module 'swagger-client'; 。 但是,当我尝试使用它时:

import * as Swagger from 'swagger-client';

class Swagger extends Swagger {
  pet: any;
  constructor() {
    super({ 
      url: config.get('spec.petstore'),
      usePromise: true
    });
  }
}

我收到错误Type 'any' is not a constructor function type

所有3条评论

这是设计使然。 唯一支持的扩展 null 形式是extends null其他任何东西,必须返回一个类型具有构造签名的值。 在这种情况下,我们将 any 作为通配符排除在外,只是因为这里比预期的用户错误更有可能。 看看是否有实际用例需要这样做会很有趣。

这是一个有趣的案例,在 TypeScript 2.0 中,我声明了一个模块 swagger-client ,如: declare module 'swagger-client'; 。 但是,当我尝试使用它时:

import * as Swagger from 'swagger-client';

class Swagger extends Swagger {
  pet: any;
  constructor() {
    super({ 
      url: config.get('spec.petstore'),
      usePromise: true
    });
  }
}

我收到错误Type 'any' is not a constructor function type

当我从索引文件导入时出现此错误,如下所示:

从 '.' 导入 {Derived, Base}

并且派生类在基类之前列在 index.ts 中:

从'./Derived'导出{Derived};
从 './Base' 导出 {Base};

我改变了顺序,它起作用了。

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

DanielRosenwasser picture DanielRosenwasser  ·  3评论

wmaurer picture wmaurer  ·  3评论

Roam-Cooper picture Roam-Cooper  ·  3评论

bgrieder picture bgrieder  ·  3评论

CyrusNajmabadi picture CyrusNajmabadi  ·  3评论