Saya akan menghindari sekarang menggunakan /// <reference path='moo.ts'/>
tetapi untuk perpustakaan eksternal di mana file definisi tidak termasuk ke dalam paket.
The reference path
memecahkan kesalahan dalam editor, tetapi tidak benar-benar berarti kebutuhan file yang akan diimpor. Oleh karena itu jika Anda menggunakan alur kerja atau JSPM, mereka mungkin mencoba mengkompilasi secara terpisah setiap file alih-alih tsc -out
ke satu file.
Dari Typescript 1.5
Awali apa yang ingin Anda ekspor di tingkat file (cakupan root)
aLib.ts
{
export class AClass(){} // exported i.e. will be available for import
export valueZero = 0; // will be available for import
}
Anda juga dapat menambahkan di akhir file apa yang ingin Anda ekspor
{
class AClass(){} // not exported yet
valueZero = 0; // not exported yet
valueOne = 1; // not exported (and will not in this example)
export {AClass, valueZero} // pick the one you want to export
}
Atau bahkan mencampur keduanya
{
class AClass(){} // not exported yet
export valueZero = 0; // will be available for import
export {AClass} // add AClass to the export list
}
Untuk impor Anda memiliki 2 opsi, pertama Anda pilih lagi apa yang Anda inginkan (satu per satu)
anotherFile.ts
{
import {AClass} from "./aLib.ts"; // you import only AClass
var test = new AClass();
}
Atau seluruh ekspor
{
import * as lib from "./aLib.ts"; // you import all the exported values within a "lib" object
var test = new lib.AClass();
}
Catatan tentang ekspor: mengekspor dua kali nilai yang sama akan menimbulkan kesalahan {export valueZero = 0; ekspor {valueZero}; // valueZero sudah diekspor ...}