Modules နှင့် Type-only Imports
7.0.2
Modules က တခြား files တွေ သုံးနိုင်တဲ့ types နဲ့ values ကို သတ်မှတ်ပါတယ်။
Code စမ်းလို့ရတဲ့ သင်ခန်းစာ ဖွင့်ရန်
ဒီစာမျက်နှာမှာ
Type Imports ကို သီးသန့် ဖော်ပြခြင်း
Type နေရာမှာပဲ သုံးမယ့် declarations အတွက် import type သုံးပါ။ JavaScript output မှာ အဲဒီ import မပါပါဘူး။ Runtime မှာ ခေါ်မယ့်၊ ဖတ်မယ့် values အတွက် ပုံမှန် import သုံးပါ။
// product.ts
export type Product = { name: string; price: number };
export const currency = "USD";
// label.ts
import { currency } from "./product.js";
import type { Product } from "./product.js";
export function label(item: Product) {
return item.name + ": " + currency + " " + item.price;
}
Runtime နဲ့ Module Settings ကိုက်ညီအောင် ထားခြင်း
Files တွေ load မယ့် environment နဲ့ကိုက်တဲ့ settings သုံးပါ။ Node ESM project မှာ compile ပြီးတဲ့ file ဆီ relative path က ပုံမှန်အားဖြင့် .js extension ပါပါတယ်။ Bundler rules က မတူနိုင်လို့ build tool အကြံပြုတဲ့ settings သုံးပါ။
// package.json: { "type": "module" }
// tsconfig.json: { "compilerOptions": { "module": "NodeNext" } }
// format.ts
export const formatLabel = (name: string) => name.toUpperCase();
// main.ts
// The import points to format.js in the compiled output.
import { formatLabel } from "./format.js";
console.log(formatLabel("Notebook"));