TypeScript は関数をエクスポートできますか? 質問する

TypeScript は関数をエクスポートできますか? 質問する

TypeScript モジュールから単純な関数をエクスポートすることは可能ですか?

これはコンパイルできません。

module SayHi {
    export function() {
    console.log("Hi");
  }
}
new SayHi();

この作業項目できないと言っているようですが、はっきり言っていません。それは不可能なのでしょうか?

ベストアンサー1

exports =この例では何をしようとしているのか分かりません。外部のモジュールですが、リンクしたコードサンプルは内部モジュール。

経験則: と書く場合はmodule foo { ... }、内部モジュールを書いていることになります。export something somethingファイルの最上位レベルで と書く場合は、外部モジュールを書いていることになります。実際に最上位レベルで と書くことはまれです(その場合、名前が二重にネストされるため)。また、最上位レベルのエクスポートを持つファイルに とexport module foo書くことはさらにまれです( は外部から参照できないため)。module foofoo

以下のことが意味を成します (各シナリオは水平線で区切られています)。


// An internal module named SayHi with an exported function 'foo'
module SayHi {
    export function foo() {
       console.log("Hi");
    }

    export class bar { }
}

// N.B. this line could be in another file that has a
// <reference> tag to the file that has 'module SayHi' in it
SayHi.foo();
var b = new SayHi.bar();

ファイル1.ts

// This *file* is an external module because it has a top-level 'export'
export function foo() {
    console.log('hi');
}

export class bar { }

ファイル2.ts

// This file is also an external module because it has an 'import' declaration
import f1 = module('file1');
f1.foo();
var b = new f1.bar();

ファイル1.ts

// This will only work in 0.9.0+. This file is an external
// module because it has a top-level 'export'
function f() { }
function g() { }
export = { alpha: f, beta: g };

ファイル2.ts

// This file is also an external module because it has an 'import' declaration
import f1 = require('file1');
f1.alpha(); // invokes f
f1.beta(); // invokes g

おすすめ記事