-
Notifications
You must be signed in to change notification settings - Fork 5.6k
DefinitelyTyped / @types not automatically resolved with deno.json #27569
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
Comments
I just encountered this with the https://github.com/denoland/react-vite-ts-template example as well. No types for react 😞. I tried setting the |
… package.json. Tracking: denoland/deno#27569
Deno twitter said it is going to be fixed this week. And now 2.2 is released. Can someone confirm is this issue fixed? I already started switching to node :hiddenpain: |
It's still being worked on by @nathanwhit, aiming to fix it in one of the patch releases. |
@korywka sorry it slipped the release - coming soon tho! |
…'t have types (#28185) Fixes #27569. Fixes #27215. This PR makes it so type resolution falls back to looking for definitely typed packages (`@types/foo`) if a given NPM package does not contain type declarations. One complication is choosing _which_ version of the `@types/*` package to use, if the project depends on multiple versions. The heuristic here is to try to match the major and minor versions, falling back to the latest version. So if you have ``` @types/foo: 0.1.0, 0.2.0, 3.1.0, 3.1.2, 4.0.0 foo: 3.1.0 ``` we would choose `@types/[email protected]` when resolving types for `foo`. --- Note that this only uses `@types/` packages if you _already_ depend on them. So a follow up to this PR could be to add a diagnostic and quickfix to install `@types/foo` if we don't find types for `foo`.
Just ran into this issue myself, but for whatever reason, it isn't working for me...
|
@ThatBlockyPenguin Can you open a new issue with a reproduction and more details about the problem? |
First off, just wanted to say thanks to everyone maintaining this project - it's been a pleasure to use.
Problem Context
In my codebase, I've found that the DX around using
@types
packages is a bit rough around the edges.In practice, most of the import statements in my codebase are written by my editor's auto-importing (Cursor), which adds the import statement without the
@ts-types
. Deno didn't warn on these, so most of our imports wound up silently typed asany
.This is partially an editor issue, though the same editor doesn't have this issue with npm/TypeScript because they don't need the comment.
Versions
Repro
Minimal test case here: https://github.com/liamfd/test_deno_types. I just did the
deno init
and then tweaked the code. More info in the repo's README.I was also able to repro this by introducing type errors to some of the example repos. e.g. just copy pasting:
Into this reported a type error as expected, but that same code in this did not.
Workaround
As a workaround, I have found that if I manage my npm dependencies in a package.json (and use the deno.json for everything else), and remove
"nodeModulesDir": "auto"
fromdeno.json
, it behaves like npm/TypeScript, where the types are just automatically resolved. May need torm -rf node_modules && deno install --reload
as well.With ^,
deno add
seems to be able to choose the right file for new deps,npm:
goes in the package.json, and the rest in deno.json, which is a nice touch.This matches the behavior of the example repos - this one has a
package.json
and my intentional type error worked, but this one does not use apackage.json
and the type error was not reported.Minimal example of the workaround: liamfd/test_deno_types#1.
Questions
@ts-types
comment on every import for packages that don't have their types set up in a Deno-compatible way? That's what I infer from the Configuring TypeScript and Node and npm support docs, but I just want to make sure I'm not doing something dumb.@ts-types
comment? Maybe using a mirror? I didn't see anything that looked like official releases for the packages I use from JSR/deno.land.Suggestions
Assuming this is working as intended, here are some options that come to mind for me:
package.json
/npm's behavior fordeno.json
managed deps. Medium+ sized projects that use packages like React have a ton of import statements, requiring the comment on every one is a lot of overhead. (Guessing this is a no-go 😅).@ts-types
comment a little friendlier:deno lint
ordeno check
) when importing something without the// @ts-types
.deno lint
rule and then encouraging users to lint on save. Maybe there's a better way to do it (e.g. leveraging lsp: quick fix for adding a// @ts-types="npm:@types/..."
directive #18884)?Please let me know if I can clarify anything. I'm happy to contribute what I can, especially docs if that's all that's needed, I just need some guidance on intended behavior/best practices. Thanks again!
The text was updated successfully, but these errors were encountered: