esm-dev / esm.sh

A fast, smart, & global CDN for modern(es2015+) web development.
https://esm.sh
MIT License
3.14k stars 151 forks source link

Lack of X-TypeScript-Types header for sub modules of react #225

Closed misogohan closed 2 years ago

misogohan commented 2 years ago

Case

I knew that Deno 1.16 supports new JSX transforms, and have tried it with https://esm.sh/react.

Minimum code:

const App = () => <h1>Hello, world!</h1>;
{
  "compilerOptions": {
    "jsx": "react-jsx",
    "jsxImportSource": "https://esm.sh/react",
    "lib": [ "dom", "deno.ns" ]
  }
}

After running deno run, I got this:

Check file:///home/misogohan/repro/component.tsx
error: TS7026 [ERROR]: JSX element implicitly has type 'any' because no interface 'JSX.IntrinsicElements' exists.
const App = () => <h1>Hello, world!</h1>;
                  ~~~~
    at file:///home/misogohan/repro/component.tsx:1:19

TS7026 [ERROR]: JSX element implicitly has type 'any' because no interface 'JSX.IntrinsicElements' exists.
const App = () => <h1>Hello, world!</h1>;
                                   ~~~~~
    at file:///home/misogohan/repro/component.tsx:1:36

Found 2 errors.

However, I can dispense with these errors when I use https://esm.sh/preact instead of react.

Cause

With the new JSX transforms, TypeScript implicitly looks up https://esm.sh/react/jsx-runtime, but because its response header does not include X-TypeScript-Types, Deno cannot find the type declaration file of it, and TypeScript cannot resolve JSX namespace.

cf. https://esm.sh/preact/jsx-runtime hasX-TypeScript-Types header, and such errors does not occur.

Solution

I confirmed that there is the type declaration at https://cdn.esm.sh/v58/@types/react/jsx-runtime.d.ts. So just to link it, I guess.

Additional info

KyleJune commented 2 years ago

It might be that if url + '/index.d.ts' does not exist, it assumes there isn't a types file for it. I found another module that it was able to add x-typescript-types header for sub modules. https://esm.sh/@heroicons/react@1.0.5/outline It's types was https://esm.sh/@heroicons/react@1.0.5/outline/index.d.ts.

Maybe having it fallback to checking '.d.ts' would be a reasonable solution that would resolve the issue of it not being able to get the types for these react submodules.

ije commented 2 years ago

now support submodule types from @types example: https://github.com/alephjs/esm.sh/tree/master/test/deno/react-jsx-runtime