簡體   English   中英

谷歌日歷 API - 打字稿類型

[英]google Calendar API - typescript types

有誰知道 node.js(或瀏覽器)的 google Calendar API(npm googleapis pkg)是否有可用於打字稿的類型。 允許在 node 或 angular 中使用強類型方法。

我找不到 @types/googleapis npm 包。 文檔中也沒有任何內容。

歡迎任何建議。

提前致謝。

-- 文檔說它是本地支持的,不需要單獨的包。 Doc - 部分 Typescript但是,當我嘗試時,如文檔中所述

    import { google, calendar_v3 } from 'googleapis';

打字稿告訴我:[ts] 模塊 '"/home/me/myProject/functions/node_modules/googleapis/build/src/index"' 沒有導出成員 'calendar_v3'。 [2305]

當我查看 googleapis/build/src/index.d.ts 時,我看到了 GoogleApis,它指向所有 apis 所在的 ../apis,帶有一個包含命名空間的 v3.d.ts 文件

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

所以,很明顯,它在那里,但我錯過了一些東西......但是什么? 你如何在打字稿中使用這個庫? 一個例子將是受歡迎的。

菲利普

@justin-beckwith 的回答是正確的,但檢索類型也值得舉個例子:

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;

您絕對可以將這個庫與 TypeScript 一起使用。 它是用 TypeScript 編寫的! 這是一個基本示例:

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

這是獲取對 API 特定子部分的引用的正確方法:)

希望這可以幫助!

嘗試遵循此Angular 6 – Google Drive Api v3 示例,我嘗試使用本教程將 Google API 集成到我的 angular 應用程序中。 它不是@types/googleapis而是@types/gapi

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

取而代之的gapi.client.drive你會打電話gapi.client.calendar

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM