18
6

More than 5 years have passed since last update.

オリジナルの型定義ファイルの置き方

Last updated at Posted at 2018-02-15

手順

  1. 適当に型定義ファイルをおく
    • 今回はtsconfig.jsonからみて app/assets/javascripts/types/custom/index.d.tsという場所に型定義ファイルを追加した
    • app/assets/javascripts/types/index.d.tsのように、types直下に型定義ファイルをおくと下記設定ではresolveできないので注意
  2. tsconfig.jsoncompilerOptions.typeRootsを編集する
  3. おわり
tsconfig.json
{
  "compilerOptions": {
    // "traceResolution": true,      // 各種名前解決の様子をログに残してくれる. 様子を追うときに利用した
    "typeRoots": [
      "node_modules/@types",         // デフォルトではここだけ読む.忘れず書き添える
      "app/assets/javascripts/types" // ここを読むように変更
    ]
  },
}

これだけで(グローバルに使いたい)オリジナルの型定義を一箇所にまとめて書けるようになった.

app/assets/javascripts/types/index.d.ts
declare var gon: any
type id = string | number

みたいなものを書いておける。

疑問

しかしこのことを調べると、typeRootsではなくbaseUrl, pathsを使うみたいな記述が多く、その使い分けをちょっとよく分かっていない。

18
6
1

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
18
6