如何在typescript中获取对象的接口



我正试图为我创建一个处理gmail的类,问题是我想要所述对象的typescript intellisense。然而,这并没有给我提供我试图在构造函数中创建的谷歌对象的打字:

class Gmail {
private auth: any;
google = null;
messageList: emailListType = ('' as unknown) as emailListType;
constructor(auth) {
this.google = google.gmail({ version: 'v1', auth: this.auth }).users;
}
async getEmailsInLabel(label: string) {
//@ts-ignore
this.messageList = await this.google.messages.list({
labelIds: [label],
userId: 'me',
});
}
}

我也试着去看这个函数的类型定义,发现了这个:

import { AuthPlus } from 'googleapis-common';
import { gmail_v1 } from './v1';
export declare const VERSIONS: {
'v1': typeof gmail_v1.Gmail;
};
export declare function gmail(version: 'v1'): gmail_v1.Gmail;
export declare function gmail(options: gmail_v1.Options): gmail_v1.Gmail;
declare const auth: AuthPlus;
export { auth };

谷歌对象的类型似乎是gmail_v1.gmail,然而每当我尝试导入gmail_v1.gmail界面时,typescript都会说我无法从d.ts文件导入。

我也试过这个:

class Gmail {
private auth: any;
google = google.gmail({ version: 'v1', auth: this.auth }).users;
messageList: emailListType = ('' as unknown) as emailListType;
constructor(auth) {
this.auth = auth;
}
async getEmailsInLabel(label: string) {
//@ts-ignore
this.messageList = await this.google.messages.list({
labelIds: [label],
userId: 'me',
});
}
}

可以打字,但不运行函数,但让这一点变得毫无意义,我还试图在构造函数内部重新分配google属性,使其等于执行google.gmail函数:不起作用。

TLDR:如何将类中google属性的类型设置为gmail_v1.gmail界面的类型?如何从外部库获取对象的接口?构建这样的类以便构造函数将类型传递给属性的最佳实践是什么?

从'..导入{gmail_v1}/node_modules/googleapis/build/src/apis/gmail/v1';正在为我工作。

查看代码,您要查找的接口似乎没有被库导出。如果未导出,则无法导入。

不能导入声明文件(.d.ts(。编辑器等工具使用它们来验证代码并帮助开发人员。

最新更新