• typescript:声明文件


    声明文件

    typescript中以.d.ts 为后缀的文件被称为声明文件。
    声明文件的作用是为当前ts代码提供类型支持。
    声明文件中包含当前ts代码中所依赖的类型。
    类型声明可以不用把js重构为ts就能使用类型系统

    声明文件分为三种类型

    • typescript内置的声明文件
    • 安装的第三方声明文件
    • 自定义的声明文件

    语言内置的声明文件

    当我们安装typescript的同时就已经安装了内置的声明文件,typescript内置的声明文件存放在typescript安装目录下的lib文件夹中。

    第三方声明文件

    如果在项目中使用某一个第三方库,通常也需要安装该库的声明文件,这样typescript能够对该库的代码进行类型检查,同时也提供该库的类型提示。

    类库分为: 全局类库,模块化类库,UMD类库
    有些类库有安装文件,有些类 库没有声明文件。
    以jQuery为例,jQuer有声明文件可以直接安装

    npm install @types/jQuery
    
    • 1

    不过有的第三方库自带声明文件有的第三方库没有内置声明文件,需要自己手动安装。

    对于没有内置类型声明的第三方库,可以到DefinitelyTyped查找其声明文件。

    如何判断一个库有没有内置声明文件呢,就是到node_modules目录中该库的目录下找是否存在.d.ts文件,如果存在则说明该库内置声明文件,否则没有内置,需要手动安装。

    自定义声明文件

    如果安装的第三方库既没有提供内置的声明文件,而且在DefinitelyTyped也找不到声明文件,就需要自己写。

    检查是否有声明文件

    typeSearch网站查询,输入要查询的类库名称即可

    编写声明文件

    全局类库的声明文件

    全局类库:

    function globalLib(options){
    	console.log(options)
    }
    globalLib.version="1.0.0"
    globalLib.doSomething=function (){
    	console.log("globalLib do something")
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    声明文件

    declare function globalLib(options:globalLib.Options):void{
    	declare namespace globalLib{
    		const version = "1.0.0"
    		function doSomething():void
    		interface Options{
    			[key:string]:any
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    模块类库

    const version ="1.0.0"
    function doSomething(){
    	console.log("moduleLib do something")
    }
    function mobuleLib(options){
    	console.log(options)
    }
    moduleLib.version=version
    moduleLib.doSomething=doSomething
    module.exports=modulesLib
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    声明文件

    declare function moduleLib(options:Options){
    	interface Options{
    		[key:string]:any
    	}
    	declare namespace moduleLib{
    		const version :string
    		function doSomething():void
    	}
    }
    export = mobuleLib
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    UMD类库

    (function (root,factory){
    	if(typeof define === "function" && define.amd){
    		define(factory)
    	}else if(typeof module === "object" && module.exports){
    		module.exports = factory();
    	}else{
    		root.umdLib = factory()
    	}
    })(this,function (){
    	return {
    		versionL:"1.0.0",
    		doSomething(){
    			console.log("umdLib do something");
    		}
    	}
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    声明文件

    declare namespace umdLib{
    	const version:string
    	function doSomething():void
    }
    export as namespace umdLib
    export = umdLib
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    Cocoa Application-基础
    使用EasyExcel后端导出excel
    CommunityToolkit.Mvvm8.1 消息通知(4)
    多线程之生产者与消费者
    windows mysql安装
    Linux:firewalld防火墙-小环境实验(3)
    【实操干货】如何开始用Qt Widgets编程?(五)
    css预编译器--sass
    利用浏览器将Markdown导出为HTML、PDF
    DenseNet网络理解
  • 原文地址:https://blog.csdn.net/qq_40850839/article/details/127656146