与 import/export 有关的问题与打字

Issue related to import/export with typings

我有 JS 文件,想为它编写类型。

import { ApiService }  from './src/ApiService'

然后我写打字然后导出

   export declare class ApiService {
     constructor(adapter: any, options: any);
     on:(evt, cb) => any;
     extend: (opts) => any;
}

error TS2440: Import declaration conflicts with local declaration of ApiService

我该如何解决?

将您的声明放入文件 ApiService.d.ts(但不需要关键字 declare):

// src/ApiService.d.ts
export class ApiService {
  constructor(adapter: any, options: any);
  on:(evt, cb) => any;
  extend: (opts) => any;
}

注意TS定义文件名必须与JavaScript文件名相同:ApiService.d.ts描述了一个JavaScript文件ApiService.js.

然后,导入它:

// test.ts
import { ApiService }  from './src/ApiService'

应该可以。