0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Deno で npm パッケージの型がつかない時の対処法

Posted at

結論

import 文の上に @ts-types="npm:@types/XXX" を書く。

// @ts-types="npm:@types/argparse"
import { ArgumentParser } from 'npm:argparse';

解説

まず、Deno でも npm パッケージ本体に型が含まれている場合は何もしなくても型がつく。

import Fastify from 'npm:fastify'; // Fastify はパッケージに型が含まれている

const fastify = Fastify({ logger: true }); // 型がつくしVSCodeでIntellisenseが働く

しかし、パッケージXXX本体に型が含まれていない場合、DefinitlyTyped の型定義パッケージ @types/XXX を使って型を追加するということがよくある。

Node.js の場合はその型定義パッケージを npm install しておけば特に何もせずとも型がつくようになるが、Denoではそうはいかず「結論」のように明示的に型定義を指定する必要がある。

@ts-types の公式ドキュメントでの記述は以下。

別の書き方

@ts-types@deno-types でも動く。何が違うのかは不明。

// @deno-types="npm:@types/argparse"
import { ArgumentParser } from 'npm:argparse';

公式ドキュメントでの @deno-types の記述は以下。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?