Skip to content

[ts5.8.2+node16] error TS2307: Cannot find module 'trusted-types/lib' or its corresponding type declarations #4943

Closed

Description

@AviVahl

Which package(s) are affected?

Lit Core (lit / lit-html / lit-element / reactive-element)

Description

Upgrading to [email protected], on a project that uses node16 module/moduleResolution, results in the following error:

> tsc

node_modules/lit-html/development/lit-html.d.ts:7:34 - error TS2307: Cannot find module 'trusted-types/lib' or its corresponding type declarations.

7 import type { TrustedHTML } from 'trusted-types/lib';
                                   ~~~~~~~~~~~~~~~~~~~


Found 1 error in node_modules/lit-html/development/lit-html.d.ts:7

Reproduction

mkdir lit-type-issue
cd lit-type-issue
npm init -y
npm i typescript lit
npx tsc --init --skipLibCheck false --module node16 --moduleResolution node16
echo "import * as lit from 'lit'; console.log(lit);" > index.ts
npx tsc --noEmit

Workaround

  1. turn on skipLibCheck to avoid type-checking .d.ts files
  2. avoid using the newest typescript version
  3. avoid node16 moduleResolution

Is this a regression?

Yes. This used to work, but now it doesn't.

Affected versions

failing with typescript 5.8.2, passes with 5.7.3

Browser/OS/Node environment

Browser: irrelevant
OS: Fedora 41
Node: 22.14.0
npm: 10.9.2

Activity

  1. justinfagnani commented on Mar 2, 2025

    @justinfagnani
    Collaborator

    Can you run npm ls trusted-types in your workspace?

  2. AviVahl commented on Mar 2, 2025

    @AviVahl
    Author

    Can you run npm ls trusted-types in your workspace?

    I believe you mean @types/trusted-types... as this package is what's installed as a dependency of lit-html itself.
    Yes, @types/trusted-types is installed and available in node_modules (you can reproduce this via the reproduction steps).

    The root cause of the issue, I think, is that the import is not specific enough to target @types/trusted-types/lib/index.d.ts in node16 module resolution. Newer TypeScript version seems to be more strict about this, enforcing it for @types` as well.

    The import should probably say import type { TrustedHTML } from 'trusted-types/lib/index.js'; to work properly in all modes (and typescript versions).

  3. freshp86 commented on Mar 12, 2025

    @freshp86

    I am hitting the same issue when trying to roll Chromium's Lit version from 3.0.2 to 3.2.1. We are also using TypeScript 5.8.2. Tracking this at https://issues.chromium.org/issues/402800778.

  4. added 4 commits that reference this issue on Mar 28, 2025
    5b77661
    b55f6dd
    a8d91a9
    e469e07
  5. added a commit that references this issue on Mar 29, 2025
    0a9bc72
  6. vdegenne commented on Apr 4, 2025

    @vdegenne
    Contributor

    Still having this issue here. Is it because this change has not yet been included in the last release?

    If so, what workaround would you recommend? (when downgrading project to node resolution is not an option)

  7. freshp86 commented on Apr 8, 2025

    @freshp86

    FWIW, I have also not been able to verify that the PR above fixes the issue. I still get the error after manually patching our local lit-html copy as follows (basically replaying what the PR did?)

    diff --git a/node_modules/lit-html/lit-html.d.ts b/node_modules/lit-html/lit-html.d.ts
    index 4ef5cd6a85785..0b55a6a4b8456 100644
    --- a/node_modules/lit-html/lit-html.d.ts
    +++ b/node_modules/lit-html/lit-html.d.ts
    @@ -4,7 +4,7 @@
      * SPDX-License-Identifier: BSD-3-Clause
      */
     import type { Directive } from './directive.js';
    -import type { TrustedHTML } from 'trusted-types/lib';
    +import type { TrustedHTML } from 'trusted-types/lib/index.js';
     
     /**
      * Contains types that are part of the unstable debug API.
    

    @sorvell Are you sure this fix is sufficient?

  8. freshp86 commented on Apr 8, 2025

    @freshp86

    Actually, I disregard my previous comment, as the problem seems to have been caused by a different root cause (we were excluding @types/trusted-types/lib/ as a whole from our local node_modules/ folder. Still trying to verify if the end-to-end solution works though.

  9. freshp86 commented on Apr 9, 2025

    @freshp86

    Here are my latest findings (in case they help anyone else, also see related Chromium candidate CL here)

    1. Needed to update @types/trusted-types to a later version.

    In my original experimentation, was using

      "@types/trusted-types": "1.0.6",
      "lit": "3.2.1",
    

    This combination does not work (not sure why NPM did not throw any errors though). Need to update @types/trusted-types to a later version to work with latest Lit. Not sure what is the minimum necessary version, but I updated to the latest @types/trusted-types": "2.0.7" locally.

    1. Need to locally patch @types/trusted-types to fix a similar error.

    The error is as follows

    ../../third_party/node/node_modules/@types/trusted-types/index.d.ts:2:22 - error TS2792: Cannot find module './lib'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?
    
    2 import * as lib from "./lib";
                           ~~~~~~~
    Found 1 error in ../../third_party/node/node_modules/@types/trusted-types/index.d.ts:2
    
    build failed
    

    and is fixed by the following local patch

    diff --git a/node_modules/@types/trusted-types/index.d.ts b/node_modules/@types/trusted-types/index.d.ts
    index 18ab043a3eea4..41f1cda229af2 100644
    --- a/node_modules/@types/trusted-types/index.d.ts
    +++ b/node_modules/@types/trusted-types/index.d.ts
    @@ -1,4 +1,4 @@
    -import * as lib from "./lib";
    +import * as lib from "./lib/index.js";
     
     // Re-export the type definitions globally.
     declare global {

    Probably a bug on @types/trusted-types that should be fixed there.

    1. Needed update code that used ... instanceof TrustedHTML or similar.

    The latest @types/trusted-types version started throwing the following error

    error TS2693: 'TrustedHTML' only refers to a type, but is being used as a value here. 
    

    which required changing ... instance of TrustedHTML to ... instanceof window.TrustedHTML. This also seems like a bug, since the two statements should both be allowed (they are equivalent from a runtime perspective).

    The PR that was landed here does not seem to be necessary to fix the original error, at least in the context of the Chromium codebase.

  10. freshp86 commented on Apr 10, 2025

    @freshp86

    FYI for the purposes of Chromium, this is no longer a blocker, per the above findings. We rolled to v3.2.1 at https://chromium-review.googlesource.com/c/chromium/src/+/6441215. Thanks!

  11. AviVahl commented on Apr 12, 2025

    @AviVahl
    Author

    Verified as fixed by [email protected].

  12. added a commit that references this issue on May 7, 2026
    53d4b53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions