简体   繁体   English

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

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

I'm trying to create a class that handles gmail for me, the problem is I'd like the typescript intellisense for said object.我正在尝试为我创建一个处理 gmail 的 class,问题是我想要 typescript intellisense 用于表示 typescript intellisense 用于说 gmail。 However this is not giving me the typings for the google object I am trying to create in the constructor:然而,这并没有给我我试图在构造函数中创建的谷歌 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',
        });
    }
}

I've also tried to go to the type definition of that function, and found this:我也尝试过 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. 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.

I've also tried this:我也试过这个:

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

works for typing but doesn't run the function but making the point moot, I've also tried to reassign the google property inside of the constructor making equal to an execution of the google.gmail function: doesn't work.适用于打字但不运行 function 但没有实际意义,我还尝试重新分配构造函数内部的 google 属性,使其等于执行 google.gmail ZC1C425268E68384D1AB457 不工作:

TLDR: How can I set the type of the google property in my class to that of the gmail_v1.Gmail interface? TLDR:如何将 class 中的 google 属性类型设置为 gmail_v1.Gmail 接口的类型? How do I get the interface of an object from an external library?如何从外部库中获取 object 的接口? What are the best practices for building classes like this so that the constructor will pass the types to the properties?构建这样的类以便构造函数将类型传递给属性的最佳实践是什么?

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

Looking at the code , it seems the interface you are looking for is not exported by the library.查看代码,您正在寻找的接口似乎不是由库导出的。 If it is not exported, you cannot import it.如果未导出,则无法导入。

You cannot import declaration files ( .d.ts ).您不能导入声明文件 ( .d.ts )。 They are used by tools, such as editors, to verify code and aid developers.编辑器等工具使用它们来验证代码并帮助开发人员。

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

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