LoginSignup
1
3

More than 3 years have passed since last update.

Denoでのパッケージ読み取り方法

Last updated at Posted at 2020-05-15

「Denoで型を使う場合どうすればいいんだろう?」 と思ったので、まとめました。

本記事は https://deno.land/manual/getting_started/typescript を基に作られています

  • DenoにはNPMのような唯一のパッケージ管理システムは無い
    • そのため package.json も node_modulesも無い
  • Denoでのモジュールのimport方法はURL([* 拡張子を含む])指定import
    • import React from "https://dev.jspm.io/react/index.js";
    • 見てわかる通り、 .js という拡張子が必要
  • 型定義をする場合はグローバルとローカルで方法が異なる

[グローバルにある型を取得する場合] @deno-types= コメントを import 時に記述する

index.tsx
// @deno-types="https://servestjs.org/@v1.0.0/types/react/index.d.ts";
import React from "https://dev.jspm.io/react/index.js";

[ローカルにある型を取得する場合] /// triple-slash directiveを export時に 記述する

foo.js
/// <reference types="./foo.d.ts" />
export const foo = "foo";

それぞれのコメントを見てわかる通り、importと同じ解決ロジックを使うため .ts という拡張子が必要
また、当たり前だがこれらが必要なのは .js ファイルが絡んでいる時だけ

1
3
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
1
3