首页 > 解决方案 > 声明一个既是类型又是值的打字稿模块

问题描述

我正在为 WordPress wp.customizeAPI 输入内容。但是,我遇到了一个障碍,不知道如何声明顶级导出。

问题如下:

function xxx (a: wp.customize.Setting){

我不知道如何同时做到这些。我不是图书馆的作者,所以我无法更改它,我只想为它创建类型。我所做的是:

// 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;
}

理想情况下,我希望以与实现其他 WordPress 类型相同的方式导入类型,但这并不是绝对必要的:

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

但是,然后我不能使用类型,例如wp.customize.Setting. 我明白了error TS2503: Cannot find namespace 'wp'.

标签: javascriptwordpresstypescripttypescript-typings

解决方案


推荐阅读