Home > Mobile >  How to export a reference to a (JavaScript) class in TypeScript
How to export a reference to a (JavaScript) class in TypeScript

Time:10-19

I want to add some type information to a legacy JavaScript project. I added a d.ts-File and declared some types and some exports. The JavaScript file exports a instance and class. Like this:

const { Foo } = require('foo');

const stuff = {foo: new Foo(), FooClass: Foo};
module.exports = stuff;

This is my d.ts-file:

import { Foo } from 'foo';

export const foo: Foo;
//???How to declare the export of FooClass???

The best I got so far is export class FooClass extends Foo{}, but I would prefer not to introduce this virtual inheritence.

CodePudding user response:

The type of the constructor of a class (FooClass) is typeof ClassName (typeof Foo), so:

import { Foo } from 'foo';

export const foo: Foo;
export const FooClass: typeof Foo;
  • Related