DRYな備忘録

Don't Repeat Yourself.

TypeScriptのmodule定義とexportキーワードについてメモるよ

疑問

  • module定義の中でexportするのは結果分かる
  • exportしないとスコープどうなんねん
    • module内参照可能なのか?

メモ

module My {
    export function funcA() {
        console.log("This is funcA");
    }
    export function funcC() {

        // call unexported function

        // モジュール定義内
        funcD();

        // モジュール定義外
        // My.funcB();
        // The property 'funcB' does not exist on value of type 'typeof My'.
    }
    function funcD() {
        console.log("This is funcD");
    }
}
module My {
    function funcB() {
        console.log("This is funcB");
    }
}

My.funcA();
// My.funcB();
// The property 'funcB' does not exist on value of type 'typeof My'.
My.funcC();

// 結果
// This is funcA
// This is funcD

結論

  • exportしない場合、同一定義文内でのみ参照可能。
  • 同moduleであっても参照不可。