declare关键字
declare是描述TS文件之外信息的一种机制,它的作用是告诉TS某个类型或变量已经存在,我们可以使用它声明全局变量、函数、类、接口、类型别名、类的属性或方法以及后面会介绍的模块与命名空间。
declare关键字用来告诉编译器,某个类型是存在的,可以在当前文件中使用。
作用:就是让当前文件可以使用其他文件声明的类型。比如,自己的脚本使用外部库定义的函数,编译器会因为不知道外部函数的类型定义而报错,这时就可以在自己的脚本里面使用declare关键字,告诉编译器外部函数的类型,这样编译脚本就不会因为使用了外部类型而报错。
declare关键字可以描述变量、type或者interface命令声明的类型、class、Enum、函数、模块和命名空间。
全局声明方式
- declare var 名称: 变量
 - declare const / let 名称: ES6变量
 - declare function 名称: 方法
 - declare class 名称: 类
 - declare enum 名称: 枚举
 - declare module 名称: 模块
 - declare namespace 名称: 命名空间
 - declare interface 名称: 接口
 - declare type 名称: 类型别名
 
示例
declare namespace constant {
  export type  HttpStatusCode = httpStatusCode;
  export type HttpStatusBusinessCode1 = httpStatusBusinessCode;
  export type Gender = gender;
}
/**
 * 网络请求状态码
 */
export enum httpStatusCode {
  }
/**
 * 业务请求状态码
 */
export enum httpStatusBusinessCode {
}
export enum gender {
  //男性
  male = 0,
  //女性
  female = 1
}
export default constant; 
 
引用
import  {constant}  from '../utils/constant'; 




















