๐Ÿ“šย Reference


๐Ÿ“œย Chapter


Assertion Functions


asserts condition - ๊ธฐ๋ณธ ์›๋ฆฌ


function assert(condition: any, msg?: string): asserts condition {
  if (!condition) {
    throw new AssertionError(msg);
  }
}

์™œ ๊ทธ๋ƒฅ if๋ฌธ๋ณด๋‹ค ์ข‹์„๊นŒ? (ํƒ€์ž… ์ขํžˆ๊ธฐ)


function processInput(input: string | null) {
  // ์„ ํ–‰ ์กฐ๊ฑด ๊ณ„์•ฝ: input์€ ์ ˆ๋Œ€ null์ผ ์ˆ˜ ์—†์Œ
  assert(input !== null, "์ž…๋ ฅ๊ฐ’์ด null์ž…๋‹ˆ๋‹ค.");

  // ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ TypeScript๋Š” input์ด 'string'์ž„์„ 100% ํ™•์‹ ํ•ฉ๋‹ˆ๋‹ค.
  // ๋ณ„๋„์˜ null ์ฒดํฌ ์—†์ด ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  console.log(input.toUpperCase()); 
}