如何在typescript中正确声明全局类型



我尝试使用全局类型,但是当我这样做时,节点崩溃,没有任何真正的错误。

Myglobal.d.ts:

import { Method } from 'axios';
declare global { 
type AxiosConf = { url: string, method: Method, data: object };
}
export {};

然后,下面的代码使节点崩溃:

const postBaseline: AxiosConf = { 
url: urlObj.urlStr, 
method: urlObj.urlMethod as Method,
data: {}
};

导致[nodemon] app crashed - waiting for file changes before starting...

我的代码有什么问题?

我最终没有使用全局声明,而是使用类型的导出,然后导入它

types.d.ts:

import { Method } from 'axios';
// Axios config object
export type AxiosConf = { url: string, method: Method, data: object };

使用时:

import { AxiosConf } from "../lib/global/types"

最新更新