登录/注册
小开开
2649
占位
2
占位
0
浏览量
占位
粉丝
占位
关注
TypeScript-三斜线指令和d.ts文件
小开开
2020-12-03 17:18:29 2020-12-03
199
0

三斜线指令和 .d.ts

习惯上,常常把外部声明写在一个后缀名为 .d.ts 的声明文件中,然后用三斜线指令引入进来

// jquery.d.ts 文件
declare let $: (selector: string) => {
html: (content: string) => void;
};
// main.ts 文件
/// <reference path="./jquery.d.ts" />
$('body').html('hello world');

上述语句声明了 main.ts 依赖 jquery.d.ts 声明文件,在编译阶段,被依赖文件 jquery.d.ts 将被包含进来,就像将被依赖文件的源码展开在依赖声明处一样:

// main.ts文件等价于将代码在三斜线指令处展开
declare let $: (selector: string) => {
html: (content: string) => void;
};
$('body').html('hello world');

三斜线指令中需要注意的是 path 类型和 types 类型的区别:

/// <reference path="./jquery.d.ts" />
/// <reference types="node" />
  • path 类型声明的是对本地文件的依赖,包含路径信息
  • types 类型声明的是对 node_modules/@types 文件夹下的类型的依赖,不包含路径信息
暂无评论