当前位置: 首页 > ai >正文

Typescript d.ts 文件作用

1. 声明全局变量

declare const API_URL: string;declare global {interface Window {customProperty: string; // 自定义属性类型}
}
必须在模块内使用

2. 声明模块

declare module 用于为模块(如文件类型或第三方库)提供类型定义,尤其是当模块没有自己的类型定义时。

declare module '*.png' {const src: string;export default src;
}

3. 声明命名空间

//声明文件
declare namespace IUser {interface User {name: string;age: number;}type I = string;
}

4. 声明类型

declare 可以用来声明类型别名或接口

declare type User = {id: number;name: string;
};

5. 声明外部库

当使用没有类型定义的第三方库时,可以用 declare 为其提供类型定义。

import  aa from 'my-lib';不能在模块内使用declare module 'my-library' {export function myFunction(): void;
}
//声明文件
declare namespace IUser {interface User {name: string;age: number;}type I = string;
}// 声明全局变量
declare global {interface Window {customProperty: string; // 自定义属性类型}
}export {IUser};
declare module 'my-lib' {export function myFunction(): void;
}
declare module '*.vue' {import type {DefineComponent} from 'vue';const component: DefineComponent<{}, {}, any>;export default component;}https://cn.vuejs.org/guide/typescript/options-api.html#augmenting-global-properties
import {IUser} from './types/env';
import  aa from 'my-lib';
import * as bb  from './aaa.vue'
let user: IUser.User = {name: 'John Doe',age: 30,
};let i: IUser.I = '1';
// window.window.customProperty = 'Hello, world!'; // 使用自定义属性

http://www.xdnf.cn/news/2848.html

相关文章:

  • 《算法笔记》10.5小节——图算法专题->最小生成树 问题 E: Jungle Roads
  • 水平布局组 (Horizontal Layout Group)
  • ​钓鱼网页散播银狐木马,远控后门威胁终端安全
  • 基于卫星遥感监测火情的技术原理解析
  • .NET 平台详解
  • conda相关操作
  • 存储器分类
  • 物联网平台厂商有哪些?2025物联网平台推荐?国内有哪些比较好的物联网平台?
  • 初识Redis · 分布式锁
  • 海外社交App用户留存秘籍:构建高粘性社区的算法与运营实战
  • MATLAB画一把伞
  • 数据结构|并查集
  • 将infinigen功能集成到UE5--在ue里面写插件(python和c++)
  • 从数据到决策:安科瑞EIoT如何让每一度电“清晰可见”?
  • IF:22.7 InfoMat:一种用于智能假肢的机器学习辅助多功能触觉传感器
  • 考研408-计算机组成原理冲刺考点(4-5章)
  • 方案精讲:71页央国企信创化与数字化转型规划实施【附全文阅读】
  • Logback 日志框架详解
  • 【自然语言处理与大模型】LangChain大模型应用框架入门②
  • Selenium 与 Playwright:浏览器自动化工具的深度对比
  • ComfyUI 学习笔记,案例1:2_pass_txt2img
  • 案例速成GO+Socket,个人笔记
  • WEBSTORM前端 —— 第2章:CSS —— 第4节:盒子模型
  • 【AI News | 20250429】每日AI进展
  • 破茧成蝶:一家传统制造企业的年轻化转型之路
  • VS Code + Linux 远程开发 go
  • 2025年具身智能科技研报
  • C++函数模板基础
  • 【专题五】位运算(1):常见位运算操作总结
  • DeepSeek: 探索未来的深度学习搜索引擎