Skip to content

声明文件

  • 【declare】用法
ts
declare var jQuery: (selector: string) => any
jQuery('#id')
  • 【xxx.d.ts】声明文件案例
ts
type HTTPMethods = 'GET' | 'POST' | 'PUT'
declare function myFetch<T = any>(url: string,method: HTTPMethods, data?: any): Promise<T>

// 往myFetch上面挂载变量
declare namespace myFetch {
    const get: <T = any>(url: string) => Promise<T>
    const post: <T = any>(url: string, data: any) => Promise<T>
}

export = myFetch
  • 上面声明文件对应的.js使用文件
ts
myFetch<string>('test','POST',{name: 'xx'}).then(data => {})

myFetch.get<number>('test').then(data => {})

苏ICP备20040768号