MoonHighway / deno-2-course

This repo contains all of the files for a course that covers the basics of Deno 2.0.
19 stars 2 forks source link

Error on 11-typescript-and-jsx-in-deno #4

Open warcayac opened 1 week ago

warcayac commented 1 week ago

I have a Deno 2 project with these files:

deno.json

{
    "compilerOptions": {
        "jsx": "react",
        "jsxFactory": "React.createElement",
        "jsxFragmentFactory": "React.Fragment"
    },
    "imports": {
        "react": "npm:react@^18.3.1"
    }
}

App.tsx

import React from "https://esm.sh/react@18.3.1";

const App = () => {
    return <h1>Hello from JSX</h1>;
};

console.log(App());

this project is running as expected with no errors, But if I use this line:

import React from "react";

I get the following error on H1 tags:

JSX element implicitly has type 'any' because no interface 'JSX.IntrinsicElements' exists.deno-ts(7026)
⚠ Error (TS7026)  | 
JSX element implicitly has type any because no interface JSX.IntrinsicElements exists.

I tried adding @types/react package, then no error was showed however when running the program I get this error:

error: Could not resolve 'npm:@types/react@18.3.12'.

Caused by:
    [ERR_INVALID_PACKAGE_TARGET] Invalid "exports" main target {"types@<=5.0":{"default":"./ts5.0/index.d.ts"},"types":{"default":"./index.d.ts"}} defined in the package config /home/warcayac/.cache/deno/npm/registry.npmjs.org/@types/react/18.3.12/package.json imported from 'file:///home/warcayac/DataDisk/Projects/Programming/Deno/_tutorials/MoonHighway/deno-2-course/11-typescript-and-jsx-in-deno/App.tsx'; target must start with "./"

I don't know what's happening. Any ideas?

jarrodu commented 1 week ago

These compiler options worked for me.

...
  "compilerOptions": {
    "jsx": "react-jsx",
    "jsxImportSource": "npm:react@^18.3",
    "jsxImportSourceTypes": "npm:@types/react@^18.3"
  }, 
...

See https://docs.deno.com/runtime/reference/jsx/#jsx-automatic-runtime-(recommended)

warcayac commented 1 week ago

I just went with:

{
    "compilerOptions": {
        "jsx": "react",
        "jsxFactory": "React.createElement",
        "jsxFragmentFactory": "React.Fragment"
    },
    "imports": {
        "react": "https://esm.sh/react@18.3.1"
    }
}