Export relevant types in a namespace for local TypeScript development
(Kinda) Fixes https://github.com/grahamearley/FirestoreGoogleAppsScript/issues/131.
Recap: the issue at stake is that right now, it’s impossible to grab the library’s types to please TypeScript’s type checker, for the repository is merely a group of .ts files, eventually projecting conflict-prone JS values in the global namespace.
Solution: Use ES modules (import/export everywhere) to prevent from polluting the global namespace with values and types, which is a best practice for a GAS library; and rework index.d.ts to properly expose the library’s types through a dedicated namespace, the same way the OAuth 2 library does it (from the guys at Google).
How to then use the library for TypeScript developpment:
- Install the library just for grabbing its types locally:
npm -i -D firestore_google-apps-script # once/if published to npmjs.com
# or
npm -i -D grahamearley/FirestoreGoogleAppsScript # in the meantime
- Within your project, in a .d.ts file of your making that’s included in tsconfig.json, add:
declare const FirestoreDataSource: FirestoreGoogleAppsScript.FirestoreApp; // globally available
// or
declare global { const FirestoreDataSource: FirestoreGoogleAppsScript.FirestoreApp; } // globally available
// or
export const FirestoreDataSource: FirestoreGoogleAppsScript.FirestoreApp; // you’ll have to import it in files
Note: although you don’t need to, it may clarify your intent adding:
/// <reference types="../node_modules/firestore_google-apps-script/typings/index.d.ts" />
Note: you may rename FirestoreDataSource, it’s just a Clean Architecture naming convention I’m following, which maps the userSymbol I dediced to use in appsscript.json. If you are manually enabling the lib through the Google Apps Script UI, it will actually be be called FirestoreApp by default, which does not conflict with the one that’s namespaced.