You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
[ts5.8.2+node16] error TS2307: Cannot find module 'trusted-types/lib' or its corresponding type declarations #4943
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
turn on skipLibCheck to avoid type-checking .d.ts files
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).
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.
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.
Here are my latest findings (in case they help anyone else, also see related Chromium candidate CL here)
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.
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.
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.
Activity
justinfagnani commentedon Mar 2, 2025
Can you run
npm ls trusted-typesin your workspace?AviVahl commentedon Mar 2, 2025
I believe you mean
@types/trusted-types... as this package is what's installed as a dependency oflit-htmlitself.Yes,
@types/trusted-typesis 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.tsin 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).freshp86 commentedon Mar 12, 2025
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.
fix(lit-html): imports barrel explicitly
fix(lit-html): imports barrel explicitly
fix(lit-html): imports barrel explicitly
fix(lit-html): imports barrel explicitly
fix(lit-html): import `trusted-types/lib` barrel explicitly. (#4956)
vdegenne commentedon Apr 4, 2025
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
noderesolution is not an option)freshp86 commentedon Apr 8, 2025
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?)
@sorvell Are you sure this fix is sufficient?
freshp86 commentedon Apr 8, 2025
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 localnode_modules/folder. Still trying to verify if the end-to-end solution works though.freshp86 commentedon Apr 9, 2025
Here are my latest findings (in case they help anyone else, also see related Chromium candidate CL here)
@types/trusted-typesto a later version.In my original experimentation, was using
This combination does not work (not sure why NPM did not throw any errors though). Need to update
@types/trusted-typesto 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.@types/trusted-typesto fix a similar error.The error is as follows
and is fixed by the following local patch
Probably a bug on
@types/trusted-typesthat should be fixed there.... instanceof TrustedHTMLor similar.The latest
@types/trusted-typesversion started throwing the following errorwhich required changing
... instance of TrustedHTMLto... 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.
freshp86 commentedon Apr 10, 2025
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!
AviVahl commentedon Apr 12, 2025
Verified as fixed by [email protected].
fix(lit-html): import `trusted-types/lib` barrel explicitly. (lit#4956)