Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

typescript - Rename interface from imported type

In Google Apps Script, I'm using the Advanced Calendar Service, which uses a default symbol "Calendar", but I renamed it to "CalendarService". What do I need to do include this name change when using the type definitions for Apps Script in a TypeScript project?

Modifying the type definition file directly is something that I'd rather not do as it's an installed npm package.

// ./node_modules/@types/google-apps-script/apis/calendar_v3.d.ts
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/google-apps-script/apis/calendar_v3.d.ts

declare namespace GoogleAppsScript {
  namespace Calendar {}
  interface Calendar {}
}

declare var Calendar: GoogleAppsScript.Calendar;

As it stands, the naming creates two issues:

  1. Usage of the advanced service isn't correctly picked up by the TypeScript compiler
  2. I can't have a global Calendar (maybe an issue that can be treated separately, but I'd like to use that name)
class Calendar {
  id: string;
  constructor(id) {
    this.id = id;
  }

  getEvent(eventId): GoogleAppsScript.Calendar.Schema.Event {
    // TypeScript cannot find `CalendarService`
    return CalendarService.Events.get(this.id, eventId);
  }
}
question from:https://stackoverflow.com/questions/65852230/rename-interface-from-imported-type

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using patch-package to commit changes to the type definition file, which allows:

  • A full rename of the global Calendar variable to CalendarService.
  • A visible and automated way for changes to be applied and shared in the project.

To setup:

  1. npm i patch-package --save-dev

  2. Add "postinstall": "patch-package" to package.json scripts

  3. Rename declared variable in the type definition file:

    -declare var Calendar: GoogleAppsScript.Calendar;
    +declare var CalendarService: GoogleAppsScript.Calendar;
    
  4. npx patch-package @types/google-apps-script (or use yarn)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...