I've been exploring clasp, a tool that allows developers to work with Google Apps Script using TypeScript.
Currently, I am working on a script that converts a Google Sheet into a PDF Blob and then uploads it to Google Drive.
While the code is executing without errors, I am facing some challenges in properly handling the types of Blob versus BlobSource to satisfy TypeScript.
Setting Up
To address this, I have defined some type abbreviations at the beginning of my file:
type Sheet = GoogleAppsScript.Spreadsheet.Sheet;
type SS = GoogleAppsScript.Spreadsheet.Spreadsheet;
type GBlob = GoogleAppsScript.Base.Blob;
type GBlobSource = GoogleAppsScript.Base.BlobSource;
In one of my functions, which has the following signature:
getPdfBlob(sheet: Sheet, pdfName: string): GBlob
I'm utilizing the function like this within my code:
var pdfBlob = getPdfBlob(mySheet, 'aPdfName');
var file = DriveApp.createFile(pdfBlob);
The Issue
However, my IDE is flagging an error indicating that DriveApp.createFile
expects a parameter of type BlobSource
instead of Blob
.
Upon attempting to cast Blob
into
BlobSource</code:</p>
<pre><code>var file = DriveApp.createFile(<GBlobSource>pdfBlob);
The feedback from my IDE raises concerns:
The conversion of type 'Blob' to type 'BlobSource' could potentially be incorrect due to insufficient overlap between the two. To proceed intentionally, consider converting the expression to 'unknown' first.
The property 'getBlob' is missing in type 'Blob' but is required in type 'BlobSource'.
Despite these warnings, the code still functions as intended. However, refining my type declarations is essential for maintaining TypeScript integrity.
After reviewing the documentation, it appears that Blob
actually implements BlobSource
. Hence, it's puzzling why there are obstacles when trying to "upcast" back to BlobSource
. Could this issue possibly stem from inaccuracies in the TypeScript definitions?
I would highly appreciate any insights or guidance on addressing this matter correctly. Thank you!