Kitura
Kitura3w ago

To use @types/react, do I have to prefix all my React imports with // @deno-types ?

Re: https://docs.deno.com/runtime/fundamentals/node/#importing-types React does not ship with types, and they have to be installed at npm:@types/react. The documentation says types have to be imported like so:
// @deno-types="npm:@types/express@^4.17"
import express from "npm:express@^4.17";
// @deno-types="npm:@types/express@^4.17"
import express from "npm:express@^4.17";
Surely I don't have to actually prefix all React imports with @deno-types? It's a lot of imports across many files. Is there a deno.json solution? What if I were to upgrade react, do I have to do a search and replace across the codebase to update the types too? Any help is appreciated.
Deno
Node and npm support
In-depth documentation, guides, and reference materials for building secure, high-performance JavaScript and TypeScript applications with Deno
5 Replies
Kitura
KituraOP3w ago
It looks like Typescript has compilerOptions.types to automatically resolve @types packages. Maybe Deno could use the same feature? https://www.typescriptlang.org/tsconfig/#types
TSConfig Reference - Docs on every TSConfig option
From allowJs to useDefineForClassFields the TSConfig reference includes information about all of the active compiler flags setting up a TypeScript project.
Mark G
Mark G3w ago
Looks like there is an option you can add into deno.json... https://docs.deno.com/runtime/reference/jsx/#jsximportsourcetypes
Deno
JSX
In-depth documentation, guides, and reference materials for building secure, high-performance JavaScript and TypeScript applications with Deno
Mark G
Mark G3w ago
{
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "npm:react@^18.3",
"jsxImportSourceTypes": "npm:@types/react@^18.3"
}
}
{
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "npm:react@^18.3",
"jsxImportSourceTypes": "npm:@types/react@^18.3"
}
}
m4rc3l05
m4rc3l052w ago
i belive if you add npm:@types/react to the compilerOptions.types array it should type react imports no it does not it seems...
m4rc3l05
m4rc3l052w ago
i belive currently we need to specify the deno-types comment https://github.com/denoland/deno/issues/27062
GitHub
deno check ignores compilerOptions.types in deno.json · Issue...
Version: Deno 2.1.1 In my deno.json, I have compilerOptions and imports defined like so: { "compilerOptions": { "types": ["@testing-library/jest-dom"] }, "imports...

Did you find this page helpful?