knip icon indicating copy to clipboard operation
knip copied to clipboard

📘 How to deal with global `@types` (ambient types)

Open fregante opened this issue 10 months ago • 2 comments

Link

https://knip.dev/reference/known-issues#definitely-typed-packages-in-dependencies

Description of the issue

Is there a way for knip to automatically detect types that are automatically loaded globally? For example I have these in my ignoredDependencies

"@types/chrome",
"@types/dom-navigation",
"@types/trusted-types",
"@types/webpack-env",
"@types/gapi.client",
"@types/gapi.client.drive-v3",
"@types/gapi.client.oauth2-v2",
"@types/gapi.client.sheets-v4",

I tried searching on the website and on the repo but I was unable to find an answer.

I suppose the "right" way would be to list them in tsconfig's compilerOptions.types instead of letting TypeScript automatically include them all, but from knip's point of view they're ignored/unchecked just the same way.

fregante avatar Feb 09 '25 10:02 fregante

Not really. Can remember looking to find out how tsc does this and I was unable to let alone replicate that behavior.

Knip uses TypeScript's own ts.readConfigFile to load tsconfig.json and in --debug output there's "Definition paths" with the added DTS files. But that's incomplete.

For now you could add them as entry paths I guess.

webpro avatar Feb 10 '25 19:02 webpro

Yeah it's tricky. I assume it just loads all the types and then they're available when it encounters the global. It's unlikely you're able to link the types package to its usage.

Feel free to close if you feel there's no solution. Perhaps this should just be documented since for the longest time we just ignored the packages instead. Using compilerOptions.paths seems to be the most explicit/native way to list and track such types.

fregante avatar Feb 10 '25 21:02 fregante

FWIW, TS 6.0 will reportedly stop automatically loading these packages and default to [] instead. I expect that there will be substantial pushback though, so we'll see in October

  • https://github.com/microsoft/TypeScript/issues/54500

fregante avatar Jul 28 '25 11:07 fregante

FWIW, TS 6.0 will reportedly stop automatically loading these packages and default to [] instead. I expect that there will be substantial pushback though, so we'll see in October

It'll also be the default for tsc --init in a few weeks (already landed in the ts 5.9 rc)

lishaduck avatar Jul 28 '25 17:07 lishaduck

Closing since we know the answer now. Feel free to reopen to track it as a documentation issue.

fregante avatar Jul 31 '25 07:07 fregante