Declare a typescript module that is both a type and a value

Viewed 58

I'm making typings for the WordPress wp.customize API. However, I've hit a snag and don't know, how to declare the top-level export.

The problem is following:

  • wp.customize extends a class called Values, that is defined in the "module". On top of that, this class has a call signature (because it really isn't a typescript class but a myriad of horrible JS hacks). So you need to be able to call wp.customize()
  • wp.customize has member variables and functions. So these need to be accessible, e.g. wp.customize.get()
  • wp.customize also contains classes & types such as Setting, so you nned to be able to do
function xxx (a: wp.customize.Setting){

I don't know how to do these all at the same time. I am not the author of the library, so I can in no way change it, I just want to create typings for it. What I did is:

// index.d.ts

import {Customize} from './Customize';

declare const customize: Customize;

export = customize;



// Customize.d.ts

export interface Customize extends Values {
    Setting: typeof Setting;
    get(): any;
}

I would ideally like to import the typings in the same way other WordPress typings are implemented, but this is not strictly necessary:

declare const wp: {
  customize: typeof import("wordpress__customize");
};

However, then I can't use the types, such as wp.customize.Setting. I get error TS2503: Cannot find namespace 'wp'.

0 Answers
Related