Skip to content

Missing type paths for TypeScript 4.7 Node16 modules #6300

Closed
@andipaetzold

Description

@andipaetzold

[REQUIRED] Describe your environment

  • Operating System version: all
  • Browser version: all
  • Firebase SDK version: all
  • Firebase Product: all (auth, database, storage, etc)
  • TypeScript: 4.7.2

[REQUIRED] Describe the problem

With TypeScript 4.7 a new module was introduced to improve the ES module support: Node16 (https://devblogs.microsoft.com/typescript/announcing-typescript-4-7/#esm-nodejs). When using that module in a tsconfig.json and importing from firebase/xxx, an error is thrown because the type declaration cannot be found.

The issue is that only a global typings/types path is defined. With module: Node16 and strict: true, the path to the types must also be added to the exports field in the package.json. (At least this is the only way I got it working).

Steps to reproduce:

  1. Create a new project
  2. Install TypeScript 4.7.2
  3. Add tsconfig.json with module: Node16 and strict: true
  4. Create an index.ts where you import from firebase/xxx
  5. Run tsc

You will receive an error similar to this:

index.ts:1:28 - error TS7016: Could not find a declaration file for module 'firebase/database'. '/home/andipaetzold/dev/firebase-node16-issue/node_modules/firebase/database/dist/index.cjs.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/firebase` if it exists or add a new declaration (.d.ts) file containing `declare module 'firebase/database';`

1 import type { Query } from "firebase/database";

I've created a repository to easily reproduce the issue: https://github.com/andipaetzold/firebase-node16-issue

Relevant Code:

index.ts

import type { Query } from "firebase/database";

tsconfig.json

{
  "compilerOptions": {
    "module": "Node16",
    "strict": true,
    "skipLibCheck": true
  }
}

Solution

In order to get the repository linked above working, I had to adjust several files in the node_modules folder:

node_modules/firebase/package.json

    "./database": {
      "node": {
        "require": "./database/dist/index.cjs.js",
        "import": "./database/dist/index.mjs"
      },
      "default": "./database/dist/index.esm.js",
+     "types": "./database/dist/database/index.d.ts"
    },

node_modules/@firebase/database/package.json

"exports": {
    ".": {
      "node": {
        "import": "./dist/node-esm/index.node.esm.js",
        "require": "./dist/index.node.cjs.js"
      },
      "esm5": "./dist/index.esm5.js",
      "standalone": "./dist/index.standalone.js",
      "default": "./dist/index.esm2017.js",
+     "types": "./dist/public.d.ts"
    },
    "./package.json": "./package.json"
  },

With skipLibCheck: false in tsconfig.json, similar changes must be done in all indirectly imported firebase packages

After the adjustments, tsc does not throw and compiles successfully.

In order to fix all imports, the types need to be added to all exports.

I started on this locally, but wanted to open this issue first in case I failed using TypeScript or you are working on a fix already. Also, the solution might be added to the use_typings.js script and I didn't want to mess around with your build setup

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions