firebase-tools icon indicating copy to clipboard operation
firebase-tools copied to clipboard

Published package is missing type declarations

Open callumlocke opened this issue 5 years ago • 10 comments

[REQUIRED] Environment info

firebase-tools: 8.4.3

Platform: macOS Catalina

[REQUIRED] Test case

import * as firebaseTools from 'firebase-tools'

or

import firebaseTools from 'firebase-tools'

[REQUIRED] Steps to reproduce

Just import the 'firebase-tools' module in a TypeScript file and run tsc on it.

[REQUIRED] Expected behavior

It should come with its own types (it is written in TypeScript, after all).

[REQUIRED] Actual behavior

TypeScript complains that this module has no type declations:

Could not find a declaration file for module 'firebase-tools'. '<REDACTED>/node_modules/firebase-tools/lib/index.js' implicitly has an 'any' type.
  Try `npm install @types/firebase-tools` if it exists or add a new declaration (.d.ts) file containing `declare module 'firebase-tools';`

Sure enough, looking at the actual published package in my node_modules, it contains no .d.ts files. Why?

There's also nothing at @types/firebase-tools.

callumlocke avatar Jun 18 '20 19:06 callumlocke

@callumlocke thanks for this, seems like a totally reasonable request to make. I am not JavaScriptSmartEnough to implement it, but I imagine @bkendall has thought about this before.

One difficulty is probably that most of the commands are defined at runtime, so we'd have to do some fancy logic to generate the .d.ts file.

samtstern avatar Jun 22 '20 15:06 samtstern

ditto. Why isn't this high priority?

sgehrman avatar Jan 28 '21 01:01 sgehrman

This is something I've been toying with recently, but there are other priorities out there and, unfortunately, I'm not sure how useful automatically generated types would be. Our typing has gotten somewhat better as of late in the codebase, but I'm not sure it's ready for 'external' primetime.

Thanks for showing interest though - it does help to prioritize work! :)

bkendall avatar Jan 28 '21 01:01 bkendall

I want

ghost avatar Apr 28 '21 08:04 ghost

I'm also skeptical of the utility provided by automatically generated types for this package, but one particular use case that I think would be valuable is exposing FirebaseConfig, either directly from firebase-tools or from a separate package.

I needed access to that type in order to build a tool for managing Firebase config files. I considered using json-schema-to-typescript to consume the config schema available in this package, but it seemed a bit silly to generate TypeScript from a schema file that was itself generated from TypeScript, so for now I'm directly copying firebaseConfig.ts into my package.

https://github.com/mandalify/firebase-config-generator

jakekrog avatar Jul 29 '21 20:07 jakekrog

@jakekrog we have recently started building a JSON Schema for that file: https://github.com/firebase/firebase-tools/blob/master/schema/firebase-config.json

You can probably make some use of that! We have not yet hosted the schema anywhere, but you can grab it from GitHub at that predictable location for now.

samtstern avatar Jul 29 '21 20:07 samtstern

@samtstern thanks for the quick response! I did try using that schema with json-schema-to-typescript to essentially recreate firebaseConfig.ts, but the generated TypeScript is more convoluted and missing granular type definitions.

You can see the difference by comparing the original firebaseConfig.ts to what is generated from json-schema-to-typescript.

I do feel a bit icky copying firebaseConfig.ts directly into the package I'm working on, but certainly less icky than using a schema generated from TypeScript to recreate an approximation of types not yet exported from this package. The 'Advanced' example from the package I linked above would not be possible without those type definitions:

// firebase.config.ts

import {
    EmulatorsConfig,
    FirebaseConfig, 
    FirestoreConfig,
    FunctionsConfig,
    StorageConfig,
    generateFirebaseConfig,
} from '@mandalify/firebase-config-generator'

const AUTH_EMULATOR_PORT = 9099
const FIRESTORE_EMULATOR_PORT = 8080
const FUNCTIONS_EMULATOR_PORT = 5001

function emulatorsConfig(host: string, uiEnabled: boolean): EmulatorsConfig {
    return {
        auth: {host, port: AUTH_EMULATOR_PORT},
        functions: {host, port: FUNCTIONS_EMULATOR_PORT},
        firestore: {host, port: FIRESTORE_EMULATOR_PORT},
        ui: {enabled: uiEnabled}
    }
}

const firestore: FirestoreConfig = {
    rules: 'firestore.rules',
    indexes: 'firestore.indexes.json',
}

const functions: FunctionsConfig = {
    source: 'functions',
}

const storage: StorageConfig = {
    rules: 'storage.rules',
}

const baseConfig: FirebaseConfig = {
    firestore,
    functions,
    storage,
}

generateFirebaseConfig({
    // 'default' writes to firebase.json
    default: {
        ...baseConfig,
        functions: {
            ...functions,
            predeploy: [
                "npm --prefix functions install",
                "npm --prefix functions run lint",
                "npm --prefix functions run build"
            ]
        },
        emulators: emulatorsConfig('0.0.0.0', true),
    },
    
    // arbitrary keys write to firebase.{key}.json
    githhub: {
        ...baseConfig,
        emulators: emulatorsConfig('localhost', false),
    }
})

jakekrog avatar Jul 29 '21 20:07 jakekrog

Any update on this? Adding this would make using firebase-tools in a typescript environment much easier. Given that all other official firebase projects I've used seem to have basic Typescript support, I'm not sure why this one is lacking it.

Mitchman215 avatar Jan 05 '23 18:01 Mitchman215

I would love to see this and would be more than happy to help out if needed. I want to use the firebase-tools package inside other code and typings would go a loooong way.

yharaskrik avatar Mar 15 '23 23:03 yharaskrik

👍

crossan007 avatar May 21 '24 14:05 crossan007

+1

floooh avatar Feb 04 '25 12:02 floooh

1+

HouseOfCode0 avatar Jul 13 '25 04:07 HouseOfCode0