Skip to content
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

Missing type paths for TypeScript 4.7 Node16 modules #6300

Closed
andipaetzold opened this issue May 26, 2022 · 8 comments · Fixed by #6307
Closed

Missing type paths for TypeScript 4.7 Node16 modules #6300

andipaetzold opened this issue May 26, 2022 · 8 comments · Fixed by #6307
Labels

Comments

@andipaetzold
Copy link
Contributor

[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

@google-oss-bot
Copy link
Contributor

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

@jbalidiong
Copy link
Contributor

Hi @andipaetzold , thanks for the report. I was able to reproduce the behavior using your minimal repro. Let me check what we can do for this issue or bring someone here that can provide more context about it. I’ll update this thread if I have any information to share.

@andipaetzold
Copy link
Contributor Author

andipaetzold commented May 27, 2022

Would it help if I open a PR with my current changes to show what needs to be done to resolve the issue? Alternatively, here is a PR from the Playwright folks that resolves the same problem: microsoft/playwright#14428

About the triage: This issue affects all packages/APIs. Not only database. Database was chosen to reproduce the issue.

@jbalidiong
Copy link
Contributor

Yes, pull requests are definitely welcome! I'll notify an engineer to have this checked. Thank you for your contribution!

@hsubox76
Copy link
Contributor

This solution looks good, I just wanted to double check if there is a chance adding this field will break anyone using older versions of Node or Typescript or different configurations. It doesn't seem like it should but if you can double check that, that would be great. I think you've provided enough info so you don't need to make a demo PR to show the problem, but if you want to make an actual PR to fully fix the problem, that would be great (but totally optional).

@andipaetzold
Copy link
Contributor Author

I just created a PR (#6307) to resolve this issue in all firebase packages.

@hsubox76
Copy link
Contributor

hsubox76 commented Jun 3, 2022

This will go out in the next release which is currently scheduled for 6/23/2022 but I'm looking into doing an earlier one as that's a long ways off.

@hsubox76
Copy link
Contributor

FYI this has been released in 9.8.3

TxHawks added a commit to TxHawks/react-polymorphic-types that referenced this issue Jun 26, 2022
Since the filename pointed at by `default` and `import` (`empty`) is not identical to the filename of the `d.ts` file (`index`), the top level `types` field is not enough when using `"moduleResolution":"Node16"` and TypeScript `4.7`. 

To fix this, `types` must be explicitly specified inside the `exports` object in addition to the top level `types` field.

See [firebase/firebase-js-sdk#6300](/firebase/firebase-js-sdk/issues/6300) and [microsoft/playwright#14428](/microsoft/playwright/pull/14428)
@firebase firebase locked and limited conversation to collaborators Jul 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants