Typescript: Type 'any' is not a constructor function type

Created on 1 Sep 2015  路  3Comments  路  Source: microsoft/TypeScript

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

It's a really roundabout way to say extends null, but the types don't check out, which is awkward. You have to annotate the IIFE with a dummy ctor type like StringConstructor to satisfy the checker.

By Design

Most helpful comment

Here is an interesting case, in TypeScript 2.0 I declare a module swagger-client like:declare module 'swagger-client';. However, when I try to use it like:

import * as Swagger from 'swagger-client';

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

I get the error Type 'any' is not a constructor function type.

All 3 comments

This is by design. the only supported form of extending null is extends null anything else, has to return a value whose type has a construct signature. We have excluded any as a wildcard in this scenario, just because it is more likely a user error here than intended. it would be interesting to see if there are actual use cases where this would be needed.

Here is an interesting case, in TypeScript 2.0 I declare a module swagger-client like:declare module 'swagger-client';. However, when I try to use it like:

import * as Swagger from 'swagger-client';

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

I get the error Type 'any' is not a constructor function type.

I got this error when I imported from index file like so:

import {Derived, Base} from '.'

and the derived class was listed in index.ts before the base class:

export {Derived} from './Derived';
export {Base} from './Base';

I switched the order and it worked.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Antony-Jones picture Antony-Jones  路  3Comments

bgrieder picture bgrieder  路  3Comments

MartynasZilinskas picture MartynasZilinskas  路  3Comments

zhuravlikjb picture zhuravlikjb  路  3Comments

CyrusNajmabadi picture CyrusNajmabadi  路  3Comments