iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🙌

Enabling Type Definitions for "npm:react" in Deno

に公開
2026/06/07
1件

This is an article for Day 10 of the Deno Advent Calendar 2024.

I was planning to write about "Making module types available when importing via https in Monaco Editor," but since there were many things to consider and I couldn't summarize it well, I will instead write about the problems and solutions I encountered while using React in Deno.

The Issue Where Type Definitions Aren't Available with Just npm:react

In Deno, you can import npm packages by specifying the npm: protocol.

import React, { useState } from "npm:react";

export const App = () => {
  const [state, setState] = useState(false);

  return <div>{state}</div>;
};

However, for some npm packages, the types cannot be read properly, often resulting in errors.

The type of the variable created using setState is any

In this example, the type of the variable created using setState has become any.

How to Resolve It Using npm:@types/react

If the npm package itself does not include type definitions, you can use npm:@types/package-name like this.

// @ts-types="npm:@types/react"
import React, { useState } from "npm:react";

Types are being read and errors are gone

It used to be // @deno-types= instead of // @ts-types=, but it seems to have changed at some point. Since the "deno" name was removed, perhaps they want to make it usable more universally? I couldn't find any tools other than Deno that use this // @ts-types= directive.

How to Resolve It Using esm.sh

// @ts-types="https://esm.sh/react@19.0.0"
import React, { useState } from "npm:react";

In the case of esm.sh, you can use type definitions without specifying the corresponding @types/ package.

In Deno, corresponding @types/ packages are not automatically used. This is because there is no guarantee that the type definitions are provided for the exact same version. It seems Deno chooses not to use @types/ packages implicitly.

https://github.com/denoland/deno/issues/20455

Since the mechanism provided via @types/package-name is confusing, I wish packages would include type definitions themselves as Deno expects. I wonder why the react package doesn't include them.

It is a very Deno-like choice—avoiding simplistic implementations like Bun, which interprets packages as npm packages even without the npm: prefix.

[Extra] How to Use JSR with esm.sh

Since importing JSR packages using jsr: should never result in type definition errors, there is usually little need to use this, but like npm, there are corresponding esm.sh URLs.

import {
  encodeBase64 as encodeBase64FromJsr,
  encodeBase64Url as encodeBase64UrlFromJsr,
} from "jsr:@std/encoding";
import {
  encodeBase64 as encodeBase64FromEsm,
  encodeBase64Url as encodeBase64UrlFromEsm,
} from "https://esm.sh/jsr/@std/encoding@1.0.5";

The JSR package name follows https://esm.sh/jsr/.

The reason for specifying the version with @ is that if you don't, a blue wavy line appears. Since deno.lock files now record how redirects were handled, it might be fine not to specify it, but the wavy line is bothersome, so I specify the version name. https://esm.sh/react@19.0.0 is also the same.

Update specifier to its redirected specifier

I couldn't find it documented anywhere on esm.sh, but I found the code that parses JSR paths here.

https://github.com/esm-dev/esm.sh/blob/a7cf10c171b359e24ae4e23419ec69e321d9cec9/server/path.go#L91

Thankfully, the type definition files also specify Access-Control-Allow-Origin: * in the HTTP response headers, so you can fetch and utilize type definitions directly from the client without worrying about CORS errors.


On the other hand, this time it seemed that esm.sh was failing to recognize some of the type definitions. https://esm.sh/v135/@jsr/std__encoding@1.0.5/_dist/base64url.d.ts is unintentionally empty.

Check file:///Users/narumi/Documents/GitHub/my-repo/main.ts
error: TS2724 [ERROR]: '"https://esm.sh/v135/@jsr/std__encoding@1.0.5/_dist/mod.d.ts"' has no exported member named 'encodeBase64Url'. Did you mean 'encodeBase64'?
  encodeBase64Url as encodeBase64UrlFromEsm,
  ~~~~~~~~~~~~~~~
    at file:///Users/narumi/Documents/GitHub/my-repo/main.ts:7:3

    'encodeBase64' is declared here.
     */ export declare function encodeBase64(data: ArrayBuffer | Uint8Array | string): string;
                                ~~~~~~~~~~~~
        at https://esm.sh/v135/@jsr/std__encoding@1.0.5/_dist/base64.d.ts:16:29

It is difficult to handle TypeScript type definitions without any omissions. I have reported this in the esm.sh Issues.

https://github.com/esm-dev/esm.sh/issues/947

GitHubで編集を提案

Discussion