Skip to content

Export relevant types in a namespace for local TypeScript development #174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

chikamichi
Copy link

@chikamichi chikamichi commented Jul 19, 2025

(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:

  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
  1. 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.

@chikamichi chikamichi force-pushed the typescript-export-types branch 9 times, most recently from dc72067 to 28b801e Compare July 21, 2025 18:18
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.
@chikamichi chikamichi force-pushed the typescript-export-types branch from 28b801e to ee794ac Compare July 22, 2025 03:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to import typings for Typescript apps script development
1 participant