惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - Franky
博客园 - 叶小钗
阮一峰的网络日志
阮一峰的网络日志
Y
Y Combinator Blog
The GitHub Blog
The GitHub Blog
J
Java Code Geeks
F
Full Disclosure
V
V2EX
The Register - Security
The Register - Security
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
云风的 BLOG
云风的 BLOG
T
Troy Hunt's Blog
Microsoft Security Blog
Microsoft Security Blog
Help Net Security
Help Net Security
P
Privacy & Cybersecurity Law Blog
K
Kaspersky official blog
博客园 - 聂微东
Recorded Future
Recorded Future
I
Intezer
S
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
H
Hacker News: Front Page
SecWiki News
SecWiki News
M
MIT News - Artificial intelligence
Security Latest
Security Latest
Attack and Defense Labs
Attack and Defense Labs
A
About on SuperTechFans
aimingoo的专栏
aimingoo的专栏
宝玉的分享
宝玉的分享
T
Tenable Blog
B
Blog
N
News | PayPal Newsroom
P
Proofpoint News Feed
D
Docker
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
MyScale Blog
MyScale Blog
C
Cisco Blogs
A
Arctic Wolf
T
Threat Research - Cisco Blogs
I
InfoQ
Stack Overflow Blog
Stack Overflow Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
GbyAI
GbyAI
P
Palo Alto Networks Blog
Engineering at Meta
Engineering at Meta
Hugging Face - Blog
Hugging Face - Blog
Latest news
Latest news
Project Zero
Project Zero

icodex | 前端技术博客 | 专注 React、TypeScript、AI 与性能优化 Blog

2026-07-18-前端与AI技术周报 TypeScript 7.0 正式发布 2026-07-12-前端与AI技术周报 2026-07-05-前端与AI技术周报 MCP 还是 CLI + Skill? 2026-06-28-前端与AI技术周报 2026-06-22-AI 产品周报 2026-06-22-前端技术周报 2026-06-14-AI 产品周报 2026-06-14-前端技术周报 2026-06-08-AI 产品周报 2026-06-07-前端技术周报 2026-05-31-AI 产品周报 2026-05-31-前端技术周报 2026-05-24-AI 产品周报 2026-05-24-前端技术周报 AI Review 在 electron 中使用 chrome-devtools-mcp pnpm 的 catalogs 功能 使用 SSE 与 Streamdown 实现 Markdown 流式渲染 JavaScript Intl 对象全面指南 POML 一种管理 Prompt 的工具 如何使用 Translator Web API eslint 支持多线程并发 Lint 下一代前端工具链对比 如何开发自己的一个 shadcn 组件 shadcn-ui实现原理 IntersectionObserver API 用法 web图像格式对比(三) web图像格式对比(四) web图像格式对比(一) web图像格式对比(二) 混合内容请求限制
TypeScript全局类型定义的方式
Oxygen · 2024-04-01 · via icodex | 前端技术博客 | 专注 React、TypeScript、AI 与性能优化 Blog

TypeScript 全局类型定义或者覆盖在日常开发中经常使用,本文主要介绍几种常见的方式。

在包含在 TypeScript 类型检测文件目录内的任意位置新建xxx.d.ts文件,并使用declare global全局命名空间语法来定义覆盖类型,例如:

declare global {
/*~
*~ 定义 String 类型实例上的方法
*/
interface String {
fancyFormat(): string;
}
}

/* 文件内必须包含一个 export 语句 */
export {};

也可以使用declare global定义一些全局变量,全局类型,全局方法等:

declare global {
let timeout: number;
const version: string;

function checkCat(c: Cat, s?: VetID);

class Cat {
constructor(n: number);
readonly age: number;
purr(): void;
}

interface CatSettings {
weight: number;
name: string;
tailLength?: number;
}

/*~
*~ 定义 Window 实例上的属性或者方法
*/
interface Window {
a: string;

myFn: VoidFunction;
}
}

export {}

注意

使用declare global定义全局类型时,该文件内部必须包含至少一个export语句!

如果要对一个第三方的包覆盖其类型定义,可以使用import <module>declare module语法,例如覆盖axios的类型定义。

axios在其实例方法上定义的类型存在一个无用的泛型参数,这个参数在使用getpost等方法时必须要传,给开发带来了一些不便;同时项目自身可能会对axios进行封装,添加一些额外的config参数,因此我们可以在项目中通过以下方式来全局覆盖axios自身的类型定义:

/*  */
import axios from 'axios';

/**
* 覆盖 AxiosRequestConfig
*/
declare module 'axios' {
/**
* 自定义配置参数
*/
export interface AxiosRequestConfig {
/**
* 即时更新
*/
useTimeStamp?: boolean;
}

// https://github.com/axios/axios/issues/1510#issuecomment-525382535
export interface AxiosInstance {
request<T = any> (config: AxiosRequestConfig): Promise<T>;
get<T = any>(url: string, config?: AxiosRequestConfig): Promise<T>;
delete<T = any>(url: string, config?: AxiosRequestConfig): Promise<T>;
head<T = any>(url: string, config?: AxiosRequestConfig): Promise<T>;
post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T>;
put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T>;
patch<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T>;
}
}

在包含在 TypeScript 类型检测文件目录内的任意位置新建xxx.d.ts文件,内部可以随意定义任何类型,但是不能包含任何exportimport语句,例如:

/*~ 
*~ 全局函数
*/
declare function myLib(a: string): string;
declare function myLib(a: number): number;

/*~
*~ 全局类型
*/
interface Person {
name: string;
age: number;
}

/*~
*~ 全局对象
*/
declare namespace myLib {
/*~
*~ myLib.version
*/
const version: string;

/*~
*~ new myLib.Cat();
*/
class Cat {
constructor(n: number);
readonly age: number;
purr(): void;
}

/*~
*~ const a: myLib.CatSettings = { weight: 5, name: "Maru" };
*/
interface CatSettings {
weight: number;
name: string;
tailLength?: number;
}

/*~
*~ myLib.checkCat(c, v);
*/
function checkCat(c: Cat, s?: VetID);
}

也可以直接定义Window上的变量、方法等。

interface Window {
a: string;

myFn: VoidFunction;
}