javascript - 声明一个既是类型又是值的打字稿模块
问题描述
我正在为 WordPress wp.customize
API 输入内容。但是,我遇到了一个障碍,不知道如何声明顶级导出。
问题如下:
wp.customize
扩展了一个名为 的类Values
,该类在“模块”中定义。最重要的是,这个类有一个调用签名(因为它真的不是打字稿类,而是无数可怕的 JS hacks)。所以你需要能够打电话wp.customize()
wp.customize
有成员变量和函数。所以这些需要是可访问的,例如wp.customize.get()
wp.customize
还包含类和类型,例如Setting
,因此您需要能够做到
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'.
解决方案
推荐阅读
- python - Flask - 将方法附加到特定表?
- python - 如何为 tensorflow 创建正确的文本文件?
- elasticsearch - Elasticsearch 只过滤内部数据
- vue.js - 如何允许一个点 '.' 在 vue.js 输入字段中
- php - WordPress 仅获取选定日期范围的数据
- android - 开始为 Visa 和 Mastercard 实施 HCE 的最佳地点
- api - axios 基本授权获取
- sql - BigQuery 相关子查询 - 将数组转换为数组
- reactjs - 无法使用反应打字稿中的动态键访问状态值
- laravel - Stancl/Tenancy:我想在创建租户时自动播种