LoginSignup
75

More than 5 years have passed since last update.

TypeScriptの型定義ファイル管理ツールtsdを使ってみた

Last updated at Posted at 2013-02-12

この記事はtsdのバージョン0.3系の仕様に基づいて書かれています。最新の0.5系では仕様が爆裂変わっています。

今回はtsdについて、前回の記事 ( http://qiita.com/items/57a626aece11f9714a9b ) と同様にインストールからパッケージの管理までまとめました。

インストール

$ npm --version #=> 1.1.66
$ node --version #=> v0.8.15
$ npm install -g tsd

2013/2/12時点でv0.3.1がインストールされました。

$ tsd

Syntax: tsd [command] [args...]

The following TSD commands are included:

  Command           Description
  ----------------  ---------------------------------------------------------
  -h                Print this help message                                  
  all               Show all file definitions from repository                
  search            Search a file definition on repository                   
  install           Intall file definition. Use install* to map dependencies.
  update            Checks if any definition file needs to be updated        
  ncfg              Create a local config file                               
  info              Get lib information                 

型定義ファイルをインストールしてみる

$ pwd #=> /Users/grapswiz/tmp
$ tsd install jquery
tsd http GET https://github.com/Diullei/tsd/raw/master/deploy/repository.json
tsd http 200 https://github.com/Diullei/tsd/raw/master/deploy/repository.json
tsd http GET https://github.com/borisyankov/DefinitelyTyped/raw/master/jquery/jquery.d.ts
tsd http 200 https://github.com/borisyankov/DefinitelyTyped/raw/master/jquery/jquery.d.ts
\-- jquery@1.8 -> d.ts/DefinitelyTyped/jquery/
     \-- cc353eab-09b0-4d36-8b61-2826d084d612.key

デフォルトではカレントディレクトリのd.ts/DefinitelyTyped/に型定義ファイルがインストールされます。(ここでは/Users/grapswiz/tmp/d.ts/DefinitelyTyped/jquery/jquery.d.ts)

複数のファイルを一度にインストールする場合、tsd installに続いてスペース区切りで指定します。

$ tsd install jquery angular Bootstrap

どんな型定義ファイルがあるの?

http://www.tsdpm.com/ を参照するか、

$ tsd all

コマンドで一覧が見れます。特定のファイルを探す場合、

$ tsd search ほげほげ

が使えます。ただ、この際大文字小文字を区別するので、例えばbootstrapで探しても見つからないことがあります。

設定ファイルを書く

設定はtsd-config.jsonに書きます。このファイルがある位置でtsdコマンドを叩くとと設定が反映された状態で実行できます。

$ tsd ncfg #=> [!] Configuration file created successfully.

でテンプレートを作成できます。

tsd-config.json
{
    "localPath": "ts-definitions",
    "repositoryType": "1",
    "uri": "https://github.com/Diullei/tsd/raw/master/deploy/repository.json"
}
  • localPath: 型定義ファイルのインストール先ディレクトリ。
  • repositoryType: uriがローカルかURLなのか指定する。0ならローカル、1ならURL。
  • uri: リポジトリのある場所。

tsdの作者はDefinitelyTypedの作者でもあるのでDefinitelyTypedにあるd.tsはほとんどtsdで取得できます。

また今のところ(v0.3.1)、設定ファイルからdependencyを読み込んでインストールする(npmに対するpackage.jsonみたいな)ことはできないです。 (=>0.4系で対応されます)
なので僕は、複数パッケージをインストールするスクリプトを書いて対応してます。

build.sh
tsd install jquery angular Bootstrap

サンプルプロジェクト

設定ファイルまで含めたサンプルのプロジェクトをgithubに置きました。
https://github.com/grapswiz/tsd-sample

参考資料

https://github.com/Diullei/tsd
http://www.tsdpm.com/
https://github.com/borisyankov/DefinitelyTyped

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
75