Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extending a short hand ambient module import #12971

Closed
eggers opened this issue Dec 16, 2016 · 3 comments
Closed

Extending a short hand ambient module import #12971

eggers opened this issue Dec 16, 2016 · 3 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@eggers
Copy link

eggers commented Dec 16, 2016

TypeScript Version: 2.1.1

Code

declare module 'base';
import * as Base from 'base';

class Derived extends Base { // error TS2507: Type 'any' is not a constructor function type.
}

Expected behavior:

An easy concise way of declaring a module so that the base import can be extended

Actual behavior:

error TS2507: Type 'any' is not a constructor function type.

In order to get around the error, I have to do something like this:

declare module 'base' {
  interface Constructor {
      new (...args: any[]): this;
  }

  const _: Constructor;
  export = _;
}
@mhegazy
Copy link
Contributor

mhegazy commented Dec 16, 2016

this is the same as:

var a: any;
class C extends a {}

the type system does not know what extending any really means. does it mean that the class is any or how would a super call be...

you can cast to a class/constructor function if you need to e.g.

declare class B { }
const constructor = a as typeof B;

class C extends constructor { 
}

or just define the class on the module.

@mhegazy mhegazy added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Dec 16, 2016
@eggers
Copy link
Author

eggers commented Dec 16, 2016

@mhegazy Those solutions are all very verbose when declaring many modules as an interim step in converting a large javascript codebase.

It would be better to have the short hand module declaration export a more inclusive any object which allowed one to extend it. I can already call new on the import, but can't extend it? e.g. the following generates no error:

import * as Base from 'base';

const base = new Base();

@bradleyayers
Copy link

In my experience this is a huge point of friction for JavaScript teams working on existing code-bases dipping their toes into the statically typed world. They're almost immediately hit with the need to:

  • understand how to write types for 3rd party modules

  • understand how legacy commonjs modules are described in TS (e.g. exports = …)

  • understand @types/ and how to deal:

    • versioning
    • working around incorrect types
    • dependency vs devDependency

It would be great if instead extending an any type worked.

the type system does not know what extending any really means. does it mean that the class is any or how would a super call be...

I'd be fine with the class being an any.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

3 participants