超高速レビュー: TypeScript 0.9.1

タイトルは今日のネタから頂きました。与沢さんじゃないです。

さて本日TypeScript 0.9.1がアナウンスされたので、変更点などを見てみます。

コンパイラの高速化

0.9でさんざん遅くなったと言われていたコンパイル速度が、0.8と同等かそれ以上に高速化されたとのこと。

実際手元の小さいプロジェクトでコンパイルしたところ、たしかに2.8秒程度のコンパイルが2.0秒で終わるようになりました。ちゃんとベンチマークはしてないですが、それなりに速くなってるみたいです。

Type Queries(typeofによる型指定)

アナウンスを見ても意味がよく分からなかったのですが、仕様書(pdf)を見たらわかりました。 型指定の場所でtypeofを使って別の変数の型を指定できます。

なので、このように一時的な変数の型をコピーするのに便利、らしいです。

var a = { x: 10, y: 20 };
var b: typeof a;

前までは、一度interfaceとして定義する必要があったので、特定局面では楽になるのでは。

フィールドで宣言したメソッド内でthisが使用可能に

これはちょっと前提の説明が必要です。

まずTypeScriptはES6 Arrow Functionをサポートしているので、アロー演算子で関数定義した場合はthisが束縛されます。簡単に言うとselfとか書かないでもいいということです。そのあたりは以前に詳しく書いたのでご参照ください。

そのため、次の2つのメソッド定義では意味が異なります。

class Greeter {
    constructor(private greeting: string) { }
    greet1() {
        console.log(this.greeting);
    }
    greet2 = () => {
        console.log(this.greeting);
    }
}

var hello = new Greeter("Hello!");
var goodbye = {
    greeting: "Goodbye!",
    greet1: hello.greet1,
    greet2: hello.greet2,
};
goodbye.greet1(); // Goodbye!
goodbye.greet2(); // Hello!

goodbye.greet1()内のthisはgoodbyeオブジェクトを指すので結果は"Goodbye!"になります。一方greet2()はArrow Functionなので、thisがhelloオブジェクトを指すため"Hello!"になります。

コンパイル結果が気になる人はPlaygroundでソースをご覧下さい

と、ここで自然に書いてしまいましたが、実はTypeScrip 0.9.0ではgreet2()のようなクラス内のフィールド定義形式でのArrow Function内のthisはコンパイルエラーになっていました。これが通るようになったのが0.9.1の改善です。

アナウンスのブログによるとイベントリスナーで便利、みたいに書いてありますけど、個人的にはメソッドによってthisが違うのは気持ち悪いのでこういう書き方はしないかなーという感想です。

class Adder {
    constructor(public x: number, public y: number) { }
    addMembers = (evt: MouseEvent) => console.log(this.x + this.y);
}

var adder = new Adder(3, 4);
document.onclick = adder.addMembers;

暗黙のany型を禁止するコンパイルオプションの追加

tscに--noImplicitAnyオプションを渡すと、暗黙のany型でエラーにしてくれます。これは素晴らしい。新規のコードでは常に有効化したいオプションですね。

var x;             // Error: Variable 'x' implicitly has an 'any' type
x = "foo";
x = 2;

function f(y) {    // Error: Parameter 'y' of f implicitly as an 'any' type
    return y + 1;
}

Visual StudioASP.NETサポート

素晴らしい待望の機能ですね!使いませんが。

アナウンスに載っていなかった非互換の変更

こちらに0.9.1での非互換な変更が載っています。

tscのコマンドラインオプションの整理

かなり変更がありました。--out--out--outDirに分割されたり、--comments--removeCommentsに変更されたりしています。

他にもいくつか新しいオプションが追加されてるようですが、まだ確認してないです。

Version 0.9.0.1
Syntax:   tsc [options] [file ..] 

Examples: tsc hello.ts
          tsc --out foo.js foo.ts
          tsc @args.txt

Options:
  -c, --comments              Emit comments to output
  -d, --declaration           Generates corresponding .d.ts file
  -b, --disallowbool          Throw error for use of deprecated "bool" type
  -m, --disallowimportmodule  Throw error for use of deprecated "module" keyword when referencing an external module. Only allow "require" keyword.
  -e, --exec                  Execute the script after compilation
  -h, --help                  Print this message
  --module KIND               Specify module code generation: "commonjs" (default) or "amd"
  --nolib                     Do not include a default lib.d.ts with global declarations
  --out FILE|DIRECTORY        Concatenate and emit output to single file | Redirect output structure to the directory
  --sourcemap                 Generates corresponding .map file
  --target VER                Specify ECMAScript target version: "ES3" (default), or "ES5"
  -v, --version               Print the compiler's version: 0.9.0.1
  -w, --watch                 Watch input files
  @<file>                     Insert command line options and files from a file.
Version 0.9.1.0
Syntax:   tsc [options] [file ..] 

Examples: tsc hello.ts
          tsc --out foo.js foo.ts
          tsc @args.txt

Options:
  -d, --declaration             Generates corresponding .d.ts file
  -h, --help                    Print this message
  --mapRoot LOCATION            Specifies the location where debugger should locate map files instead of generated locations.
  -m KIND, --module KIND        Specify module code generation: "commonjs" or "amd"
  --noImplicitAny               Warn on expressions and declarations with an implied 'any' type.
  --noResolve                   Skip resolution and preprocessing
  --out FILE                    Concatenate and emit output to single file
  --outDir DIRECTORY            Redirect output structure to the directory
  --removeComments              Do not emit comments to output
  --sourcemap                   Generates corresponding .map file
  --sourceRoot LOCATION         Specifies the location where debugger should locate TypeScript files instead of source locations.
  -t VERSION, --target VERSION  Specify ECMAScript target version: "ES3" (default), or "ES5"
  -v, --version                 Print the compiler's version: 0.9.1.0
  -w, --watch                   Watch input files
  @<file>                       Insert command line options and files from a file.

モジュールローダーのデフォルトがCommonJSに変更

もともとtscのデフォルトは0.9.0でもCommonJSだったので、Visual StudioのプロジェクトのデフォルトがAMDになったということでしょうか?そちらは0.9.0時代はAMDがデフォルトだったので。すみません確認してないです。

ロードマップ

ロードマップには変更ないですね。1.0は "Stabilization" だけです。

あー、ECMAScript 6 modules syntaxだけは1.0に入れてくれないかなー。