Thuta Learning
TypeScript
BasicProgrammingအလယ်အလတ်

Strict tsconfig & Compiler Boundaries

TypeScriptသင်ခန်းစာ 525 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • `strict: true` က check အစုလိုက် ဖွင့်ပေးတဲ့ switch ဖြစ်ကြောင်းနဲ့ အဓိက check တွေ (noImplicitAny, strictNullChecks) ကို ရှင်းပြနိုင်ဖို့
  • `noUncheckedIndexedAccess` ဖွင့်ထားတဲ့အခါ `array[0]` ရဲ့ type ကို ကြိုမှန်းပြီး `undefined` ကို ကိုင်တွယ်နိုင်ဖို့
  • `exactOptionalPropertyTypes` error ကို key ချန်ထားတာ ဒါမှမဟုတ် type ပြင်တာနဲ့ ဖြေရှင်းနိုင်ဖို့
  • tsconfig.json က ဘာကို ထိန်းချုပ်လဲ၊ runtime မှာ ဘာကို မထိန်းချုပ်နိုင်လဲ ခွဲခြားနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

Strict mode ကို ရိုးရိုးလေး ပြောရရင်

tsconfig.json က compiler ကို 'ဘယ်လောက် တင်းတင်းကျပ်ကျပ် စစ်ရမလဲ' လို့ ပြောတဲ့ setting file ပါ။ Check တွေ ပိတ်ထားရင် compiler က သဘောကောင်းပါတယ်၊ ဒါပေမဲ့ bug အများစုကို ဖြတ်သန်းခွင့်ပေးလိုက်ပါတယ်။ `"strict": true` ဆိုတဲ့ line တစ်ကြောင်းက အရေးကြီးဆုံး check တွေကို တစ်ပြိုင်နက် ဖွင့်ပေးပါတယ်။

သတိရဖို့ အချက်တစ်ခုက ဘယ်လောက် strict ဖြစ်ဖြစ် compiler က သင့် source code ကိုပဲ စစ်တာပါ။ Program run နေတဲ့အချိန် API ကနေ ဝင်လာတဲ့ data ကိုတော့ မမြင်ရပါဘူး၊ type တွေက runtime မှာ ဖျက်ပြီးသား ဖြစ်နေလို့ပါ။

Problem → Why → Solution

ပြဿနာ: compile အောင်ပေမဲ့ crash ဖြစ်တယ်

အောက်က code က check တွေ ပိတ်ထားတဲ့ project မှာ error တစ်ခုမှ မပြဘဲ compile ဖြစ်ပါတယ်။ `words[0]` ရဲ့ type ကို TypeScript က `string` လို့ ယူဆပါတယ်၊ array ထဲမှာ element ရှိမရှိ မစစ်ပါဘူး။ Array အလွတ်နဲ့ ခေါ်လိုက်တာနဲ့ `first` က `undefined` ဖြစ်ပြီး `first.length` က TypeError နဲ့ crash ပါတယ်။

`double` function မှာလည်း parameter type မရေးထားလို့ `x` က အသံတိတ် `any` ဖြစ်သွားပါတယ်။ `double("5")` လို့ ခေါ်ရင် compiler က ဘာမှ မပြောဘဲ `"5" * 2` ဖြစ်တဲ့ `10` ကို ပြန်ပေးပါတယ်၊ JavaScript ရဲ့ type ပြောင်းလဲမှုကြောင့် ကံကောင်းလို့ မှန်နေတာပါ။

src/words.ts
function firstWordLength(words: string[]) {
  const first = words[0];  // typed as string, but [] gives undefined
  return first.length;     // TypeError: Cannot read properties of undefined
}

function double(x) {       // x is silently 'any'
  return x * 2;
}

firstWordLength([]);

Strict setting တွေ ဖွင့်ရင် ဘာပြောင်းလဲ

`strict` က `double(x)` ကို 'Parameter x implicitly has an any type' ဆိုပြီး ငြင်းပါတယ်။ `noUncheckedIndexedAccess` ကတော့ `words[0]` ရဲ့ type ကို `string | undefined` လို့ ပြောင်းပေးလို့ `first.length` မှာ 'first is possibly undefined' error ပြပါတယ်။ Crash နှစ်ခုစလုံးက run မတိုင်ခင် compile error ဖြစ်သွားပါတယ်။

အဓိက သဘောတရား: strict family နဲ့ compiler ရဲ့ နယ်နိမိတ်

`"strict": true` က option တစ်ခုတည်း မဟုတ်ပါဘူး၊ check အစုတစ်ခုကို ဖွင့်တဲ့ master switch ပါ။ TypeScript version အသစ်တွေမှာ strict check အသစ်တွေ ထပ်တိုးလာရင်လည်း ဒီ switch အောက်ကို ရောက်လာတတ်ပါတယ်။ ဒါကြောင့် check တစ်ခုချင်း ရေးမယ့်အစား `strict` ကို ဖွင့်ထားပြီး လိုအပ်ရင် တစ်ခုချင်း ပိတ်တာက ပိုကောင်းပါတယ်။ အရေးကြီးဆုံး check တွေက ဒီလိုပါ:

Checkဖမ်းပေးတာ
noImplicitAnyType မရေးထားလို့ any ဖြစ်သွားတဲ့ parameter
strictNullChecksnull / undefined ဖြစ်နိုင်တာကို မစစ်ဘဲ သုံးတာ
strictPropertyInitializationConstructor မှာ value မထည့်ထားတဲ့ class property
useUnknownInCatchVariablescatch (err) ထဲက err ကို any အစား unknown ထားတယ်

`strict` အောက်မှာ မပါတဲ့ ထပ်ဆောင်း check နှစ်ခုကိုလည်း ဖွင့်ထားဖို့ အကြံပြုပါတယ်။ `noUncheckedIndexedAccess` က `array[i]` နဲ့ `object[key]` ကနေ ဖတ်တဲ့ value တိုင်းကို `| undefined` ပေါင်းထည့်ပေးလို့ index မရှိတာကို စစ်ခိုင်းပါတယ်။ `exactOptionalPropertyTypes` ကတော့ `nickname?: string` ဆိုတာ 'key မရှိရင် ရတယ်' လို့ပဲ အဓိပ္ပာယ်ရစေပြီး `nickname: undefined` လို့ တိုက်ရိုက် ထည့်တာကို ငြင်းပါတယ်။

Compiler boundary ဆိုတာ tsc ရဲ့ တာဝန် ဘယ်မှာ ဆုံးလဲ ဆိုတာပါ။ tsc က source code ကို စစ်ပြီး (`noEmit` မဟုတ်ရင်) JavaScript ထုတ်ပေးပါတယ်။ Runtime မှာ `JSON.parse` ဒါမှမဟုတ် `fetch` ကနေ ရလာတဲ့ data ကိုတော့ ဘယ် setting ကမှ မစစ်ပေးနိုင်ပါဘူး၊ အဲဒါကို code နဲ့ validate လုပ်ရပါတယ်။ Bundler ဒါမှမဟုတ် `tsx` က JavaScript ထုတ်ပေးတဲ့ project မှာတော့ `noEmit: true` ထားပြီး tsc ကို type checker သက်သက်အဖြစ် သုံးပါတယ်။

ကျန်တဲ့ option တွေက check မဟုတ်ဘဲ code run မယ့် ပတ်ဝန်းကျင်ကို ဖော်ပြတာပါ။ `target` က ဘယ် JavaScript version ကို ထုတ်မလဲ (ဥပမာ `es2022`) သတ်မှတ်ပြီး `module` နဲ့ `moduleResolution` က `import` တွေကို ဘယ်လို ရေးမလဲ၊ file တွေကို ဘယ်လို ရှာမလဲ ဆုံးဖြတ်ပါတယ်။ ဒီ value တွေ မှားရင် type error မဟုတ်ဘဲ 'Cannot find module' လို error တွေ ဒါမှမဟုတ် runtime မှာ import ပျက်တာတွေ ဖြစ်တတ်ပါတယ်။ ဒါကြောင့် check setting တွေက project တိုင်း အတူတူ ဖြစ်နိုင်ပေမဲ့ ပတ်ဝန်းကျင် setting တွေကတော့ project ရဲ့ runtime ပေါ် မူတည်ပါတယ်။

tsconfig ရဲ့ တာဝန်နဲ့ နယ်နိမိတ်
  1. strict: check အစုကို ဖွင့်
  2. Extra flags: index, optional key
  3. tsc: source ကိုပဲ စစ်
  4. Runtime data: code နဲ့ စစ်

tsconfig က သင့် code ကို ဘယ်လောက် တင်းကျပ်စွာ စစ်မလဲ ဆုံးဖြတ်ပါတယ်၊ အပြင်က data ကို ဘယ်တော့မှ မစစ်ပါဘူး။

tsconfig.json ရဲ့ ပုံစံ

json
{
  "compilerOptions": {
    "strict": true,
    "<extraCheck>": true,
    "target": "<JS version to emit>",
    "module": "<module system>"
  },
  "include": ["<source glob>"]
}

`compilerOptions` ထဲမှာ check တွေနဲ့ output setting တွေ၊ `include` ထဲမှာ ဘယ် file တွေကို စစ်မလဲ ရေးပါတယ်။ JSON ဖြစ်ပေမဲ့ tsconfig.json မှာ comment နဲ့ trailing comma ထည့်ခွင့်ရှိပါတယ်။

Strict tsconfig တစ်ခု

ဒီ tsconfig က bundler (ဥပမာ Vite) က JavaScript ထုတ်ပေးတဲ့ project အတွက်ပါ။ tsc က type check ပဲ လုပ်ပြီး file မထုတ်ပါဘူး။ `"target": "es2022"` ကို သုံးထားပါတယ်၊ TypeScript 5.x version တိုင်းက လက်ခံပြီး လက်ရှိ browser နဲ့ Node version တွေကလည်း ထောက်ပံ့တဲ့ year target ပါ။ Year target က သင့် compiler သိတဲ့ နှစ်အထိပဲ ရှိပါတယ်: TypeScript 5.x မှာ `es2024` အထိပဲ ရှိလို့ `es2025` လို value ကို လက်မခံပါဘူး။

tsconfig.json
{
  "compilerOptions": {
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "exactOptionalPropertyTypes": true,
    "target": "es2022",
    "module": "esnext",
    "moduleResolution": "bundler",
    "isolatedModules": true,
    "noEmit": true
  },
  "include": ["src/**/*.ts"]
}

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. The strict family"strict": true,

    noImplicitAny, strictNullChecks စတဲ့ check အစုကို တစ်ပြိုင်နက် ဖွင့်ပါတယ်။ Problem ထဲက `double(x)` ကို ဒီ line က ဖမ်းပါတယ်။

  2. Safer indexing"noUncheckedIndexedAccess": true,

    `words[0]` ရဲ့ type က `string | undefined` ဖြစ်သွားလို့ array အလွတ် crash ကို compile error အဖြစ် ပြောင်းပေးပါတယ်။

  3. Exact optional properties"exactOptionalPropertyTypes": true,

    Optional property ကို 'key မရှိ' နဲ့ 'key ရှိပြီး value က undefined' ဆိုပြီး ခွဲခြားပါတယ်။ `{ nickname: undefined }` ကို ငြင်းပါတယ်။

  4. How code is resolved and emitted"moduleResolution": "bundler",

    Import တွေကို bundler လို ရှာဖို့ ပြောပါတယ်။ Node.js က တိုက်ရိုက် run မယ့် project ဆိုရင်တော့ `"module": "nodenext"` ကို သုံးပါ။ ဒီ setting တွေက code run မယ့် ပတ်ဝန်းကျင်နဲ့ ကိုက်ရပါမယ်။

  5. Type-check only"noEmit": true

    tsc က `.js` file မထုတ်ပါဘူး၊ bundler က ထုတ်ပေးမှာမို့ပါ။ `isolatedModules` ကလည်း file တစ်ခုချင်းစီကို type information မလိုဘဲ JavaScript ပြောင်းလို့ရအောင် အာမခံပေးပါတယ်။

Strict tsconfig အောက်မှာ code ကို ပြင်ရေးပုံ

Strict error တွေကို `!` ဒါမှမဟုတ် `as` နဲ့ ပိတ်မယ့်အစား ဖြစ်နိုင်တဲ့ case ကို တကယ် ကိုင်တွယ်ပါ။ အောက်က code က problem ထဲက function ကို `undefined` ကို စစ်ပြီး ပြင်ရေးထားတာနဲ့ optional property ကို key ချန်ထားပြီး ဖန်တီးတာကို ပြပါတယ်။ ဒီ lesson ရဲ့ tsconfig နဲ့ error မရှိဘဲ compile ဖြစ်ပါတယ်။

src/words.ts
// Compiles under the strict tsconfig from this lesson.
function firstWordLength(words: string[]): number {
  const first = words[0]; // string | undefined
  return first === undefined ? 0 : first.length;
}

type Profile = { name: string; nickname?: string };
const su: Profile = { name: "Su" }; // leave the key out instead of nickname: undefined

console.log(firstWordLength(["strict", "mode"]));
console.log(firstWordLength([]));
console.log(su.nickname ?? "(no nickname)");

ဒီ code က `6`, `0`, `(no nickname)` ကို print ထုတ်ပါတယ်။ `first === undefined` ကို စစ်ပြီးနောက် TypeScript က `first` ကို `string` လို့ narrow လုပ်ပေးလို့ `first.length` ကို ခွင့်ပြုပါတယ်။ Array အလွတ်အတွက် 0 ကို ပြန်ပေးမလား၊ error throw မလား ဆိုတာ business ဆုံးဖြတ်ချက်ပါ၊ အရေးကြီးတာက အဲဒီ case ကို တမင် စဉ်းစားပြီး ရွေးချယ်ထားတာပါ။

လက်တွေ့ project တွေမှာ

လက်တွေ့ project တွေမှာ strict mode

Project အသစ်တွေမှာ ပထမနေ့ကတည်းက `strict: true` နဲ့ စတာက အလွယ်ဆုံးပါ၊ code နည်းနည်းပဲ ရှိသေးလို့ error တွေလည်း နည်းပါတယ်။ ရှိပြီးသား JavaScript project ကို ပြောင်းတဲ့အခါမှာတော့ check တစ်ခုချင်း ဖွင့်လေ့ရှိပါတယ်: အရင် `noImplicitAny`၊ ပြီးရင် `strictNullChecks`၊ ပြီးမှ `strict` အပြည့်။ Check တစ်ခုစီရဲ့ error တွေကို ပြင်ပြီးမှ နောက်တစ်ခုကို ဖွင့်ပါတယ်။

Monorepo တွေမှာ package အားလုံးအတွက် base tsconfig တစ်ခု ထားပြီး package တစ်ခုစီက `"extends": "../../tsconfig.base.json"` နဲ့ အမွေဆက်ခံပါတယ်။ ဒါဆို strict setting တွေ package ကြား မကွဲလွဲပါဘူး။ CI မှာ `tsc --noEmit` ကို run ပြီး API boundary တွေမှာ zod လို library နဲ့ runtime validation လုပ်တာက compile time နဲ့ runtime နှစ်ဖက်လုံးကို ကာကွယ်ပေးပါတယ်။ Code review လုပ်တဲ့သူတွေကလည်း pull request ထဲမှာ `!` ဒါမှမဟုတ် `as` အသစ်တစ်ခု တွေ့ရင် မေးခွန်းထုတ်သင့်တဲ့ နေရာအဖြစ် သတိထားကြည့်တတ်လာပါတယ်။

အမှားများ

tsconfig နဲ့ ပတ်သက်တဲ့ အမှားများ

Error တွေ ပေါ်လာလို့ strict ကို ပိတ်တာ

`strict: true` ဖွင့်လိုက်တာနဲ့ error ရာချီ ပေါ်လာရင် `false` ပြန်ပြောင်းချင်စိတ် ဖြစ်ပါတယ်။ ဒါပေမဲ့ အဲဒီ error တစ်ခုစီက ဖြစ်နိုင်တဲ့ crash တစ်ခုပါ။ Strict ကို ပိတ်တာ ဒါမှမဟုတ် `value!` (non-null assertion) ကို နေရာတိုင်း ထည့်တာက bug တွေကို ဖုံးထားရုံပါပဲ။ Check တစ်ခုချင်း ဖွင့်ပြီး တဖြည်းဖြည်း ပြင်ပါ။

Compiler version မသိတဲ့ option value ကို ကူးထည့်တာ

ဥပမာ ကူးလာတဲ့ tsconfig ထဲမှာ `"target": "es2025"` ပါနေတယ် ဆိုပါစို့။ TypeScript 5.x မှာ `npx tsc` က file တစ်ခုမှ မစစ်ရသေးခင် 'Argument for --target option must be ...' error (TS6046) နဲ့ ချက်ချင်း ရပ်ပါတယ်။ Blog post ကနေ tsconfig ကူးတဲ့အခါ value တစ်ခုချင်းစီ သင့် project ရဲ့ TypeScript version မှာ ရှိမရှိ official docs မှာ စစ်ပါ။

`noEmit` ထားပြီး `dist/` ကို ရှာနေတာ

`noEmit: true` ဆိုရင် tsc က ဘာ file မှ မထုတ်ပါဘူး။ Node.js နဲ့ တိုက်ရိုက် run မယ့် project ဆိုရင် `noEmit` ကို ဖယ်ပြီး `outDir` ကို သတ်မှတ်ပါ။

Best Practices

tsconfig အတွက် အလေ့အထကောင်းများ

  • Project အသစ်တိုင်းကို `strict`, `noUncheckedIndexedAccess`, `exactOptionalPropertyTypes` ဖွင့်ပြီး စပါ
  • Option တစ်ခု ထည့်တိုင်း ဘာကြောင့် ထည့်လဲ comment တစ်ကြောင်း ရေးထားပါ (tsconfig.json မှာ comment ရေးလို့ရပါတယ်)
  • Package များတဲ့ repo မှာ base tsconfig တစ်ခုကို `extends` နဲ့ မျှသုံးပါ

Mini challenge

Mini challenge: strict-safe lookup

ဒီ lesson ရဲ့ tsconfig အောက်မှာ error မရှိဘဲ compile ဖြစ်တဲ့ function နှစ်ခု ရေးပါ။ `!` နဲ့ `as` ကို မသုံးရပါဘူး။

  • `priceOf(item: string): string` က menu မှာ ရှိရင် ဈေးနှုန်းကို ဒသမ နှစ်နေရာနဲ့၊ မရှိရင် `not on the menu` ကို ပြန်ပေးရမယ်
  • `firstAndLast(items: string[]): string` က `first ... last` ကို ပြန်ပေးပြီး array အလွတ်ဆိုရင် `(empty list)` ပြန်ပေးရမယ်
  • `noUncheckedIndexedAccess` ကြောင့် index ကနေ ဖတ်တဲ့ value တိုင်း `undefined` ဖြစ်နိုင်တာကို ကိုင်တွယ်ပါ
src/lookups.ts
// Record<string, number>: an object with string keys and number values
const prices: Record<string, number> = { tea: 1.5, coffee: 2.25 };

function priceOf(item: string): string {
  // TODO: read prices[item] - its type is number | undefined here
  // TODO: return "<item>: <price, 2 decimals>" or "<item>: not on the menu"
}

function firstAndLast(items: string[]): string {
  // TODO: items[0] and the last item may both be undefined
}

// TODO: print priceOf("tea"), priceOf("juice"), and firstAndLast for a list and []

ရလဒ်: `tea: 1.50`, `juice: not on the menu`, `setup ... types`, `(empty list)` ကို print ထုတ်ပြီး `npx tsc` က error မပြပါဘူး။

အရိပ်အမြွက် ကြည့်ရန်

Value ကို variable ထဲ အရင်ထည့်ပြီး `if (value === undefined) return ...` နဲ့ စစ်ပါ။ အဲဒီ line အောက်မှာ TypeScript က value ကို `number` (ဒါမှမဟုတ် `string`) လို့ narrow လုပ်ပေးပါတယ်။

အဖြေ ကြည့်ရန်
src/lookups.ts
const prices: Record<string, number> = { tea: 1.5, coffee: 2.25 };

function priceOf(item: string): string {
  const price = prices[item]; // number | undefined
  if (price === undefined) return `${item}: not on the menu`;
  return `${item}: ${price.toFixed(2)}`;
}

function firstAndLast(items: string[]): string {
  const first = items[0];
  const last = items[items.length - 1];
  if (first === undefined || last === undefined) return "(empty list)";
  return `${first} ... ${last}`;
}

console.log(priceOf("tea"));
console.log(priceOf("juice"));
console.log(firstAndLast(["setup", "strict", "types"]));
console.log(firstAndLast([]));

Quick Quiz 1 / 3

`noUncheckedIndexedAccess: true` ဖွင့်ထားပြီး `words: string[]` ဆိုရင် `const first = words[0];` ရဲ့ type က ဘာလဲ?

Quick Quiz 2 / 3

အောက်က check တွေထဲက ဘယ်တစ်ခုကို `"strict": true` က မဖွင့်ပေးလဲ?

Quick Quiz 3 / 3

`exactOptionalPropertyTypes` ဖွင့်ထားပြီး `type Profile = { name: string; nickname?: string }` ဆိုရင် ဘယ် value က error ပေးလဲ?

မှတ်ထားရမယ့် အချက်များ

  • `strict: true` က noImplicitAny, strictNullChecks စတဲ့ check အစုကို ဖွင့်တဲ့ master switch ပါ
  • `noUncheckedIndexedAccess` နဲ့ `exactOptionalPropertyTypes` ကို သီးသန့် ထပ်ဖွင့်ရပါတယ်
  • Strict error တွေကို `!`, `as` နဲ့ မဖုံးဘဲ `undefined` case ကို တကယ် ကိုင်တွယ်ပါ
  • `target`, `module`, `moduleResolution` က code run မယ့် ပတ်ဝန်းကျင်နဲ့ compiler version နဲ့ ကိုက်ရပါမယ်
  • tsc က source code ကိုပဲ စစ်ပါတယ်၊ runtime data ကို code နဲ့ validate လုပ်ရပါတယ်

TypeScript Compiler Options — TypeScript

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Strict tsconfig & Compiler Boundaries | Thuta Learning