google Calendar API - typescript types

|▌冷眼眸甩不掉的悲伤 提交于 2020-07-07 11:08:25

问题


Does anybody know if the google Calendar API (npm googleapis pkg) for node.js (or browser) has types available that could be used in typescript. Allowing a strongly typed approach in node or angular.

I could not find a @types/googleapis npm package. Nor anything in the doc.

Any advise welcome.

Thanks in advance.

-- The documentation says it is natively supported, no need for separate package. Doc - section Typescript BUT, when I try, as stated in the doc

    import { google, calendar_v3 } from 'googleapis';

Typescript tells me : [ts] Module '"/home/me/myProject/functions/node_modules/googleapis/build/src/index"' has no exported member 'calendar_v3'. [2305]

When I look in googleapis/build/src/index.d.ts I see GoogleApis, that point to ../apis where all the apis are, with a v3.d.ts file including the namespace

    * @namespace calendar
    * @type {Function}
    * @version v3
    * @variation v3

So, obviously, it is there, but I am missing something ... but what? How can you use this library in typescript? An example would be welcome.

Philippe


回答1:


You can absolutely use this library with TypeScript. It's written in TypeScript! Here's a basic example:

import {google} from 'googleapis';
const calendar = google.calendar('v3');

That's the right way to get a reference to a particular subsection of the API :)

Hope this helps!




回答2:


@justin-beckwith's answer is correct, but retrieving the types also deserves an example:

import { calendar_v3, google } from 'googleapis';
import { OAuth2Client, Credentials } from 'google-auth-library';
import Calendar = calendar_v3.Calendar;
import Schema$Event = calendar_v3.Schema$Event;

const auth: OAuth2Client = new google.auth.OAuth2(...);
const calendar: Calendar = google.calendar({ version: 'v3', auth });
const schemaEvent: Schema$Event = (await calendar.events.get({ calendarId, eventId })).data;



回答3:


Try following this Angular 6 – Google Drive Api v3 example, I tried this tutorial to integrate Google APIs to my angular app. It is not @types/googleapis but @types/gapi.

npm install --save @types/gapi
npm install --save @types/gapi.auth2
npm install --save @types/gapi.client.drive

Instead of gapi.client.drive you will call gapi.client.calendar.



来源:https://stackoverflow.com/questions/53306382/google-calendar-api-typescript-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!