Skip to content

Commit b835f02

Browse files
fix(calendar): resolve type errors
Resolved TypeScript type errors in the calendar/quickstart/quickstart.gs file. - Added JSDoc `@typedef` for CalendarEvent and CalendarEventDateTime to define the object structures. - Added a check to ensure the Calendar advanced service is enabled. - Cast the error object in the catch block to the Error type. - Added checks for undefined `events` and `event.start` to prevent runtime errors.
1 parent 275a497 commit b835f02

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

calendar/quickstart/quickstart.gs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,28 @@
1414
* limitations under the License.
1515
*/
1616
// [START calendar_quickstart]
17+
/**
18+
* @typedef {Object} CalendarEventDateTime
19+
* @property {string} [dateTime]
20+
* @property {string} [date]
21+
*/
22+
23+
/**
24+
* @typedef {Object} CalendarEvent
25+
* @property {string} summary
26+
* @property {CalendarEventDateTime} start
27+
*/
28+
1729
/**
1830
* Lists 10 upcoming events in the user's calendar.
31+
* @return {void}
1932
* @see https://developers.google.com/calendar/api/v3/reference/events/list
2033
*/
2134
function listUpcomingEvents() {
35+
// Add a check for the Calendar advanced service.
36+
if (!Calendar.Events) {
37+
throw new Error('Please enable the Calendar advanced service.');
38+
}
2239
const calendarId = 'primary';
2340
// Add query parameters in optionalArgs
2441
const optionalArgs = {
@@ -33,12 +50,15 @@ function listUpcomingEvents() {
3350
// call Events.list method to list the calendar events using calendarId optional query parameter
3451
const response = Calendar.Events.list(calendarId, optionalArgs);
3552
const events = response.items;
36-
if (events.length === 0) {
53+
if (!events || events.length === 0) {
3754
console.log('No upcoming events found');
3855
return;
3956
}
4057
// Print the calendar events
41-
for (const event of events) {
58+
for (const event of /** @type {CalendarEvent[]} */ (events)) {
59+
if (!event.start) {
60+
continue;
61+
}
4262
let when = event.start.dateTime;
4363
if (!when) {
4464
when = event.start.date;
@@ -47,7 +67,7 @@ function listUpcomingEvents() {
4767
}
4868
} catch (err) {
4969
// TODO (developer) - Handle exception from Calendar API
50-
console.log('Failed with error %s', err.message);
70+
console.log('Failed with error %s', /** @type {Error} */ (err).message);
5171
}
5272
}
5373
// [END calendar_quickstart]

0 commit comments

Comments
 (0)