param不接受orderBy:' startTime'?

时间:2017-10-05 00:39:34

标签: angularjs typescript google-api

您好我正在使用Google API在TypeScript中编写一个AngularJS,试图拨打电话获取Google日历事件,语法在文档中定义:https://developers.google.com/google-apps/calendar/v3/reference/events/list 但出于某种原因gapi.client.calendar.events.list(params)

不接受参数orderBy: 'startTime'

getCalendarEvents() {
    return gapi.client.calendar.calendarList.list().then(response => {
        let calendars = response.result.items;
        if (calendars === undefined) return [];
        console.log(calendars);
        let requests = calendars.map(calendar => {
            //let params = { calendarId: calendar.id, timeMin: (new Date()).toISOString(), showDeleted: false, singleEvents: true, maxResults: 10 };
            let params = { 
                calendarId: calendar.id, 
                timeMin: (new Date()).toISOString(), 
                showDeleted: false, 
                singleEvents: true, 
                orderBy: 'startTime' };
            return gapi.client.calendar.events.list(params).then(response => {
                console.log(response.result.items);
                let events: any = response.result.items.sort((a, b) => { // sorting array of events by date
                    if (a.start.date) a.start['newdate'] = a.start.date;
                    if (b.start.date) b.start['newdate'] = b.start.date;
                    if (a.start.dateTime) a.start['newdate'] = a.start.dateTime.slice(0, 10);
                    if (b.start.dateTime) b.start['newdate'] = b.start.dateTime.slice(0, 10);
                    if (a.start['newdate'] > b.start['newdate']) return 1;
                    if (a.start['newdate'] < b.start['newdate']) return -1;
                    return 0;
                });
                return events.map(event => {
                    let calendarEvent: CalendarEvent = {
                        title: event.summary,
                        htmlLink: event.htmlLink,
                        start: new Date(event.start.date == undefined ? event.start.dateTime == undefined ? '' : event.start.dateTime : event.start.date),
                        end: new Date(event.end.date == undefined ? event.end.dateTime == undefined ? '' : event.end.dateTime : event.end.date),
                        allDay: false,
                        color: calendar.backgroundColor
                    };
                    return calendarEvent;
                });

            });
        });
        return this.$q.all(requests);
    });
}

为什么这不起作用?

  

file:&#39; file:///d%3A/Projects/Homepage2/app/google.service.ts'   严重性:&#39;错误&#39;消息:&#39;类型的参数&#39; {calendarId:string;   timeMin:string; showDeleted:boolean; singleEvents:boolean;排序依据:   STRI ...&#39;不能分配给类型的参数   #&39; EventsListParameters&#39 ;.财产类型&#39; orderBy&#39;是   不相容。       输入&#39; string&#39;不能分配类型&#39;&#34; startTime&#34; | &#34;更新&#34; |未定义&#39;&#39; at:&#39; 166,61&#39;来源:&#39; ts&#39;

其中params定义如下:

  interface EventsListParameters {
    calendarId: string;
    alwaysIncludeEmail?: boolean;
    iCalUID?: string;
    maxAttendees?: integer;
    maxResults?: integer;
    orderBy?: EventsOrder;
    pageToken?: string;
    privateExtendedProperty?: string;
    q?: string;
    sharedExtendedProperty?: string;
    showDeleted?: boolean;
    showHiddenInvitations?: boolean;
    singleEvents?: boolean;
    syncToken?: SyncToken;
    timeMax?: datetime;
    timeMin?: datetime;
    timeZone?: string;
    updatedMin?: datetime;
  }

和EventsOrder的定义如下:

  type EventsOrder =
    // Order by the start date/time (ascending). This is only available when querying single events (i.e. the parameter singleEvents is True)
    'startTime' |
    // Order by last modification time (ascending).
    'updated';

1 个答案:

答案 0 :(得分:2)

修复

orderBy: 'startTime'更改为orderBy: 'startTime' as 'startTime'

更多

它被推断为一个字符串。在这里,我用断言强迫它推断为文字。