簡體   English   中英

如何在打字稿中正確聲明全局類型

[英]How to properly declare a global type in typescript

我嘗試使用全局類型,但是當我這樣做時,節點崩潰而沒有任何真正的錯誤。

我的global.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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM