声明文件: 主要是解决JS 在TS使用问题。当我们引入JS文件(包括第三方库),声明文件就可以对编辑器进行类型检查和智能提示。

在声明文件中,不能定义具体的实现

类型:
  1. 简答的声明:

关键字:declare
基础类型: 元组:tuple 数组 [] string number boolean enmu枚举

*   [`declare var`](https://github.com/xcatliu/typescript-tutorial/blob/master/basics/declaration-files.md#declare-var)声明全局变量
*   [`declare function`](https://github.com/xcatliu/typescript-tutorial/blob/master/basics/declaration-files.md#declare-function)声明全局方法
*   [`declare class`](https://github.com/xcatliu/typescript-tutorial/blob/master/basics/declaration-files.md#declare-class)声明全局类
*   [`declare enum`](https://github.com/xcatliu/typescript-tutorial/blob/master/basics/declaration-files.md#declare-enum)声明全局枚举类型
*   [`declare namespace`](https://github.com/xcatliu/typescript-tutorial/blob/master/basics/declaration-files.md#declare-namespace)声明(含有子属性的)全局对象
*   [`interface`和`type`](https://github.com/xcatliu/typescript-tutorial/blob/master/basics/declaration-files.md#interface-he-type)声明全局类型
*   [`export`](https://github.com/xcatliu/typescript-tutorial/blob/master/basics/declaration-files.md#export)导出变量
*   [`export namespace`](https://github.com/xcatliu/typescript-tutorial/blob/master/basics/declaration-files.md#export-namespace)导出(含有子属性的)对象
*   [`export default`](https://github.com/xcatliu/typescript-tutorial/blob/master/basics/declaration-files.md#export-default)ES6 默认导出
*   [`export =`](https://github.com/xcatliu/typescript-tutorial/blob/master/basics/declaration-files.md#export-1)commonjs 导出模块
*   [`export as namespace`](https://github.com/xcatliu/typescript-tutorial/blob/master/basics/declaration-files.md#export-as-namespace)UMD 库声明全局变量
*   [`declare global`](https://github.com/xcatliu/typescript-tutorial/blob/master/basics/declaration-files.md#declare-global)扩展全局变量
*   [`declare module`](https://github.com/xcatliu/typescript-tutorial/blob/master/basics/declaration-files.md#declare-module)扩展模块
*   [`/// <reference />`](https://github.com/xcatliu/typescript-tutorial/blob/master/basics/declaration-files.md#san-xie-xian-zhi-ling)三斜线指令
declare getData(id:number):string;
declare getData(id:number):any;
declare getData(id:number):string|number|underfine;

2.声明文件中,返回得JSON数据可以单独定义类型,基础类型还可以取别名。

declare type Result = {
  name: string,
  age: string,
  gender: boolean|string,
  extra?: any
}
declare getAccount(id:number): Result
声明文件中的类
含有子类
declare function Account(id: number): Account;
declare interface Account {
    getExtra(): Account.ExtraData
    name: string
    id: number
}

declare namespace Account {
    interface ExtraData {
        loginTime: number
    }
    const TypeOfUser: string
    function login(id: number): any
}
方法的重载: 声明文件允许出现多个相同名称的方法

小羊裤脚
30 声望2 粉丝

卑微前端,在线求助!