firebase / firebase-tools

The Firebase Command Line Tools
MIT License
3.97k stars 917 forks source link

Published package is missing type declarations #2378

Open callumlocke opened 4 years ago

callumlocke commented 4 years ago

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

samtstern commented 4 years ago

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

sgehrman commented 3 years ago

ditto. Why isn't this high priority?

bkendall commented 3 years ago

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! :)

ghost commented 3 years ago

I want

jakekrog commented 2 years ago

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

samtstern commented 2 years ago

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

jakekrog commented 2 years ago

@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),
    }
})
Mitchman215 commented 1 year ago

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.

yharaskrik commented 1 year ago

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.

crossan007 commented 1 month ago

👍