4

I'm writing a Google Apps Script (GAS) using the clasp tool, which lets you locally develop TypeScript files that compile to Google Scripts.

I imported Google Script type definitions by running npm i -S @types/google-apps-script, and my IDE (VS Code) does indeed seem to understand the Google Script types. I cannot, however, seem to annotate my variables with these type definition. For example,

let ss: Spreadsheet;
ss = SpreadsheetApp.getActive();

Is this possible to annotate my code with these imported type definitions?

TheMaster
  • 45,448
  • 6
  • 62
  • 85
Dustin Michels
  • 2,951
  • 2
  • 19
  • 31
  • Possible duplicate of [Enabling autocomplete for Google Apps Script in locally-installed IDE](https://stackoverflow.com/questions/49015874/enabling-autocomplete-for-google-apps-script-in-locally-installed-ide) – TheMaster Oct 16 '18 at 00:26
  • Related, follow-up question for me: [Abbreviate lengthy TypeScript Types](https://stackoverflow.com/questions/53198823/abbreviate-lengthy-typescript-types) – Dustin Michels Nov 15 '18 at 19:38

1 Answers1

6

Custom type annotations can be used like this:

var ss: GoogleAppsScript.Spreadsheet.Spreadsheet;
  • namespace: GoogleAppsScript
  • module: Spreadsheet
  • interface: Spreadsheet
TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • Man! You saved me a lot of trouble. I was giving up on having typed objects in my code. This is really great thank you. – Ashkan Jul 08 '20 at 16:31
  • @Ashkan You shouldn't need this, if your ide is configured properly. Everything should just automatically be inferred. https://stackoverflow.com/questions/49015874/enabling-autocomplete-for-google-apps-script-in-locally-installed-ide – TheMaster Jul 08 '20 at 16:32
  • I'm using Typescript in Intellij Idea and the solution in that SO answer did not work for me. The only way I could get type inferring was your solution, using the fully qualified name of the Spreadsheet type. – Ashkan Jul 09 '20 at 05:41
  • @Ashkan Jetidea specific answer is [here](https://stackoverflow.com/a/59339109/). Try to troubleshoot why types aren't inferred. – TheMaster Jul 09 '20 at 05:52
  • @Ashkan https://www.jetbrains.com/help/idea/running-and-debugging-typescript.html#ws_ts_run_debug_server_side – TheMaster Jul 09 '20 at 05:53
  • I did install the library in intellij, that was some hidden feature I didn't know about. But still no luck, just `GoogleAppsScript.Spreadsheet.Sheet` got better colors :) If I use `Sheet` instead of `GoogleAppsScript.Spreadsheet.Sheet` no type is inferred and it shows error. Anyway I'm ok with this as long as I have autocomplete and intellij shows no error! – Ashkan Jul 09 '20 at 06:13
  • @Ashkan Consider asking a new question tagging intellij. In my environment, `GoogleAppsScript.Spreadsheet.Sheet` works fine. – TheMaster Jul 09 '20 at 07:36