Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google Calendar API - typescript types

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

like image 819
philippe.kienner Avatar asked Nov 14 '18 18:11

philippe.kienner


2 Answers

@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;
like image 68
corolla Avatar answered Oct 11 '22 22:10

corolla


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!

like image 31
Justin Beckwith Avatar answered Oct 11 '22 23:10

Justin Beckwith