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

推荐订阅源

T
The Exploit Database - CXSecurity.com
C
Cyber Attacks, Cyber Crime and Cyber Security
Forbes - Security
Forbes - Security
The Last Watchdog
The Last Watchdog
F
Full Disclosure
GbyAI
GbyAI
G
Google Developers Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
阮一峰的网络日志
阮一峰的网络日志
T
The Blog of Author Tim Ferriss
C
Check Point Blog
S
Security @ Cisco Blogs
H
Help Net Security
N
News | PayPal Newsroom
D
DataBreaches.Net
L
LINUX DO - 最新话题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
U
Unit 42
WordPress大学
WordPress大学
月光博客
月光博客
Security Latest
Security Latest
V
V2EX
Schneier on Security
Schneier on Security
美团技术团队
Y
Y Combinator Blog
G
GRAHAM CLULEY
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Securelist
The Cloudflare Blog
Engineering at Meta
Engineering at Meta
P
Proofpoint News Feed
O
OpenAI News
Cisco Talos Blog
Cisco Talos Blog
Martin Fowler
Martin Fowler
N
News and Events Feed by Topic
小众软件
小众软件
S
Schneier on Security
Webroot Blog
Webroot Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Visual Studio Blog
T
Threatpost
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Scott Helme
Scott Helme
N
Netflix TechBlog - Medium
有赞技术团队
有赞技术团队
W
WeLiveSecurity
S
SegmentFault 最新的问题
Cloudbric
Cloudbric
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
M
MIT News - Artificial intelligence

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