繁体   English   中英

如何在 typescript 中获取 object 的接口?

[英]How can I get the interface of an object in typescript?

我正在尝试为我创建一个处理 gmail 的 class,问题是我想要 typescript intellisense 用于表示 typescript intellisense 用于说 gmail。 然而,这并没有给我我试图在构造函数中创建的谷歌 object 的类型:

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',
        });
    }
}

我也尝试过 go 到 function 的类型定义,发现了这个:

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 };

The google object seems to be of type gmail_v1.Gmail, However whenever I try to import the gmail_v1.Gmail interface typescript says I cannot import from d.ts files.

我也试过这个:

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',
            });
        }
    }

适用于打字但不运行 function 但没有实际意义,我还尝试重新分配构造函数内部的 google 属性,使其等于执行 google.gmail ZC1C425268E68384D1AB457 不工作:

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

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

查看代码,您正在寻找的接口似乎不是由库导出的。 如果未导出,则无法导入。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM