Export relevant types in a namespace for local TypeScript development#174
Open
chikamichi wants to merge 1 commit intograhamearley:mainfrom
Open
Export relevant types in a namespace for local TypeScript development#174chikamichi wants to merge 1 commit intograhamearley:mainfrom
chikamichi wants to merge 1 commit intograhamearley:mainfrom
Conversation
dc72067 to
28b801e
Compare
Using ES modules (import/export everywhere) prevents from polluting the
global namespace with values and types, which is a best practice for a
GAS library.
Using a namespace for the core API typings allows for a better DX when
using the library in other GAS projects.
Use it like this:
1. 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
2. In a .d.ts file of your making that’s included in tsconfig.json, add:
import { FirestoreGoogleAppsScript } from "firestore_google-apps-script/typings";
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.
28b801e to
ee794ac
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
(Kinda) Fixes #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:
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 theuserSymbolI dediced to use in appsscript.json. If you are manually enabling the lib through the Google Apps Script UI, it will actually be be calledFirestoreAppby default, which does not conflict with the one that’s namespaced.