Skip to content

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

Closed
liamfd opened this issue Jan 6, 2025 · 7 comments · Fixed by #28185
Closed

DefinitelyTyped / @types not automatically resolved with deno.json #27569

liamfd opened this issue Jan 6, 2025 · 7 comments · Fixed by #28185
Assignees

Comments

@liamfd
Copy link

liamfd commented Jan 6, 2025

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 as any.

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

❯ deno --version
deno 2.1.4 (stable, release, aarch64-apple-darwin)
v8 13.0.245.12-rusty
typescript 5.6.2

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:

import { useEffect } from 'react';

const foo: string = useEffect; // this assignment should report a type error, but doesn't
console.log(foo);

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" from deno.json, it behaves like npm/TypeScript, where the types are just automatically resolved. May need to rm -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 a package.json and the type error was not reported.

Minimal example of the workaround: liamfd/test_deno_types#1.

Questions

  1. Just to confirm, is the intention that users add the @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.
  2. Is my workaround the intended behavior for Deno?
  3. Is there another convention I'm missing that allows you to avoid repeating the @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:

  1. If my workaround (or some other convention) is the recommended way to do this, update the docs mentioned above to reference it (happy to contribute this). If not:
  2. Mirror the package.json/npm's behavior for deno.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 😅).
  3. Make the usage of the @ts-types comment a little friendlier:
    1. Update the docs mentioned above to explicitly call out the difference in behavior with npm. It's implied with the current docs, but as someone coming from the npm ecosystem, it would helped me if it were really hammered home (happy to contribute this).
    2. Add an error (either from deno lint or deno check) when importing something without the // @ts-types.
    3. Document / add a means of automatically inserting the comments. Easiest to me seems to be an autofixable 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!

@liamfd liamfd changed the title Automatically resolve DefinitelyTyped / @types DefinitelyTyped / @types not automatically resolved with deno.json Jan 6, 2025
@joshdales
Copy link

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 jsximportsourcetypes in the deno.json compilerOptions but that didn't work either - your workaround did though, really helped us out!

@D1no
Copy link

D1no commented Feb 9, 2025

Can confirm in Deno 2.1.9 — very annoying.

Image

Using Deno, Vite, React, Emotion

D1no added a commit to D1no/projectorassist that referenced this issue Feb 9, 2025
@korywka
Copy link

korywka commented Feb 19, 2025

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:

@bartlomieju
Copy link
Member

It's still being worked on by @nathanwhit, aiming to fix it in one of the patch releases.

@ry
Copy link
Member

ry commented Feb 19, 2025

@korywka sorry it slipped the release - coming soon tho!

nathanwhit added a commit that referenced this issue Feb 19, 2025
…'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`.
@ThatBlockyPenguin
Copy link

Just ran into this issue myself, but for whatever reason, it isn't working for me...
If it's of any help, the output of ctrl+shift+p Deno: Info is:

deno 2.2.2 (release, x86_64-pc-windows-msvc) | vscode_deno 3.43.4 | vscode 1.97.2

@nathanwhit
Copy link
Member

nathanwhit commented Feb 25, 2025

Just ran into this issue myself, but for whatever reason, it isn't working for me... If it's of any help, the output of ctrl+shift+p Deno: Info is:

deno 2.2.2 (release, x86_64-pc-windows-msvc) | vscode_deno 3.43.4 | vscode 1.97.2

@ThatBlockyPenguin Can you open a new issue with a reproduction and more details about the problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants