【TS】TypeScript声明文件(.d.ts)的使用

前言

当我们在TS文件中需要引入外部库时,编译时是无法判断传入参数的类型的,所以我们需要在引入前加入一个声明文件来帮助ts判断类型。
当然现在大部分库都自带有自己的声明文件,一般在@types目录下。

使用场景

在ts文件中对引用的外部库做类型判断;
制作npm包时,书写自己的声明文件,需要在package.jsontyping/types字段注册声明文件的路径;
不使用ts时,也可以添加声明文件与(自己的)的模块存放在同一目录下,简单做一下数据结构体,对IDE参数声明也有用哦。

引用声明文件的几种方法

与调用的ts文件放在同一目录下;
在声明文件tsconfig.jsoninclude/files字段下添加声明文件的路径;
在这里插入图片描述
配置.eslintrc.js

module.exports = {
	root: true,
	env: {
		browser: true,
		es2021: true,
		node: true,
	},
	parser: 'vue-eslint-parser',
	parserOptions: {
		ecmaVersion: 12,
		parser: '@typescript-eslint/parser',
		sourceType: 'module',
	},
	extends: ['plugin:vue/vue3-essential', 'plugin:vue/essential', 'eslint:recommended'],
	plugins: ['vue', '@typescript-eslint'],
	// --------此处配置,可让vscode不报红------
	overrides: [
		{
			files: ['*.ts', '*.tsx', '*.vue'],
			rules: {
				'no-undef': 'off',
			},
		},
	],
	rules: {
		// http://eslint.cn/docs/rules/
		// https://eslint.vuejs.org/rules/
		'@typescript-eslint/ban-ts-ignore': 'off',
		'@typescript-eslint/explicit-function-return-type': 'off',
		'@typescript-eslint/no-explicit-any': 'off',
		'@typescript-eslint/no-var-requires': 'off',
		'@typescript-eslint/no-empty-function': 'off',
		'@typescript-eslint/no-use-before-define': 'off',
		'@typescript-eslint/ban-ts-comment': 'off',
		'@typescript-eslint/ban-types': 'off',
		'@typescript-eslint/no-non-null-assertion': 'off',
		'@typescript-eslint/explicit-module-boundary-types': 'off',
		'vue/custom-event-name-casing': 'off',
		'vue/attributes-order': 'off',
		'vue/one-component-per-file': 'off',
		'vue/html-closing-bracket-newline': 'off',
		'vue/max-attributes-per-line': 'off',
		'vue/multiline-html-element-content-newline': 'off',
		'vue/singleline-html-element-content-newline': 'off',
		'vue/attribute-hyphenation': 'off',
		'vue/html-self-closing': 'off',
		'vue/no-multiple-template-root': 'off',
		'vue/require-default-prop': 'off',
		'vue/no-v-model-argument': 'off',
		'vue/no-arrow-functions-in-watch': 'off',
		'vue/no-template-key': 'off',
		'vue/no-v-html': 'off',
		'vue/comment-directive': 'off',
		'vue/no-parsing-error': 'off',
		'vue/no-deprecated-v-on-native-modifier': 'off',
		'vue/multi-word-component-names': 'off',
		'no-useless-escape': 'off',
		'no-sparse-arrays': 'off',
		'no-prototype-builtins': 'off',
		'no-constant-condition': 'off',
		'no-use-before-define': 'off',
		'no-restricted-globals': 'off',
		'no-restricted-syntax': 'off',
		'generator-star-spacing': 'off',
		'no-unreachable': 'off',
		'no-multiple-template-root': 'off',
		'no-unused-vars': 'error',
		'no-v-model-argument': 'off',
		'no-case-declarations': 'off',
		'no-console': 'off',
		'no-debugger': 'off',
	},
};

实例

以外部库fs为例(假设fs没有自己的声明文件)

  • fs.d.ts
declare module 'fs' {
    function readFileSync(path: string | number, options?: { encoding?: string; flag?: string; } | null): string;
}
  • MyTest.ts
import * as fs from 'fs'

const body = fs.readFileSync('../@types/fs.d.ts', {encoding: 'utf8'});
console.log(body);

总结

声明文件一般只能声明外部引入的npm包;
声明文件一定要用declare module 'fs'显式的声明自己的外部库名称;

TypeScript中,.d.ts文件是用来描述JavaScript库或模块的声明文件。它的作用是为了在开发过程中提供代码提示、类型检查以及代码文档化等功能。 首先,我们需要确保项目中安装了TypeScript。然后,我们可以在项目中创建一个.d.ts文件,命名规则一般是将库的名称与后缀.d.ts结合,比如"jquery.d.ts"。 接下来,我们需要在.d.ts文件中编写对应库的声明。对于常见的库,往往有很多社区已经编写好的声明文件可供使用,我们可以在DefinitelyTyped上搜索并下载已有的声明文件。 如果找不到已经存在的声明文件,我们可以自行编写。一般来说,我们需要定义库的各种类型、接口、函数以及其他使用方法。可以通过查阅库的官方文档来了解库的API,并根据需要进行适当的声明。 完成编写后,我们可以将.d.ts文件放置在项目中合适的位置,例如与库文件在同一目录下,或者在项目根目录下的一个名为"typings"的文件夹中。 在项目中使用该库时,TypeScript会自动根据.d.ts文件提供代码提示和类型检查。我们只需要在需要使用库的文件中导入库的模块,并按照库的API进行使用即可。 总结来说,使用.d.ts文件的流程包括:安装TypeScript、创建.d.ts文件、编写库的声明、下载或编写完成声明文件后,将其放置在合适的位置,然后在项目中使用库的模块并享受TypeScript的强大功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卸载引擎

如果对您有帮助,请赏个饭吃

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值