0

我正在为 airtable 编写一个定义文件,不幸的是,他们只导出一个这样的类:

...

module.exports = Airtable;

因此,我的airtable.d.ts文件如下所示:

declare module "airtable" {
    export type MyType = { ... };

    export class Airtable {
        ...
    }

    export = Airtable;
}

当我导入Airtable课程时,效果很好:

import Airtable = require("airtable");
...
new Airtable(...)

但我也找不到导入的方法MyType

let a: Airtable.MyType;

导致此错误:

'Airtable' 仅指一种类型,但在这里用作命名空间

和:

import { MyType } from "airtable";

导致这些错误:

模块“airtable”没有导出成员“MyType”
模块“airtable”解析为非模块实体,无法使用此构造导入

知道如何在继续使用export =and 的同时导入其他导出的类型import/require吗?
谢谢。

4

1 回答 1

1

So the answer really depends on how you're going to use the types. If you're using them in your own project, you need a file (called <whatever>.d.ts) that declares a module called "airtable". In that, you'll need to export something. Since you're exporting a class, you have to use the export = X syntax rather than export X because you're changing the entire export object rather than adding a property. on the export object (more on that in a sec). As for the types, outside the module in your .d.ts file, you can also decalre a type that will become globally available. If that rubs you the wrong way (or you're worried about conflicts), you can put your type into a module as well. Since it's a typescript-only thing, it doesn't have to be backed by any js code. You can then import/export it like normal:

// my.d.ts

declare module 'airtable' {
  class Airtable {
    constructor(opts?: MyType)
  }
  export = Airtable
}

declare type MyType = string | number

declare module 'AirtableTypes' {
  export type MyString = string
  export type MyNumber = number
}

and usage

// index.ts
import Airtable from 'airtable'
import AirtableTypes from 'AirtableTypes'

const a = new Airtable('a')

const n: MyType = 3

const s: AirtableTypes.MyString = '3'

If you want to add types to DefinitelyTyped (which I'm sure they would appreciate!) you can follow the guide here to author the declaration file.

It'll point you at

You (correctly) noted that Airtable exports a single class, which doesn't play super nicely with TS. There's some discussion here. Either way, the above guide will point you at module-class.d.ts, which lets you declare an exported class and accompanying types. You couldn't use this above, as the format is only available when definition files live in the module root or @types/<module>.

/*~ This declaration specifies that the class constructor function
 *~ is the exported object from the file
 */
export = Airtable

/*~ Write your module's methods and properties in this class */
declare class Airtable {
  constructor(opts?: Airtable.AirtableMethodOptions)
}

/*~ If you want to expose types from your module as well, you can
 *~ place them in this block.
 */
declare namespace Airtable {
  export interface AirtableMethodOptions {
    endpointUrl: string
  }
}
于 2018-05-23T23:53:08.127 回答