๐Ÿ’ป์›น(Web)/React

[TypeScript]TypeScript ์ดํ•ดํ•˜๊ธฐ(1) - ํƒ€์ž… ์ง‘ํ•ฉ / ์ถ”๋ก  / ๋‹จ์–ธ

stonesy 2024. 9. 4. 11:02
728x90

โœจํƒ€์ž…๊ณผ ์ง‘ํ•ฉ

ํƒ€์ž…์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ’์„ ํฌํ•จํ•˜๋Š” ์ง‘ํ•ฉ์œผ๋กœ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํƒ€์ž… ๊ฐ„์—๋Š” ์„œ๋กœ ํฌํ•จ ๊ด€๊ณ„(์Šˆํผ-๋ถ€๋ชจ ํƒ€์ž…, ์„œ๋ธŒ-์ž์‹ ํƒ€์ž…)์ด ์กด์žฌํ•œ๋‹ค.

 

โœ”ํƒ€์ž… ํ˜ธํ™˜์„ฑ

A์™€ B ๋‘๊ฐœ์˜ ํƒ€์ž…์ด ์กด์žฌํ•  ๋•Œ A ํƒ€์ž…์˜ ๊ฐ’์„ B ํƒ€์ž…์œผ๋กœ ์ทจ๊ธ‰ํ•ด๋„ ๊ดœ์ฐฎ์€์ง€ ํŒ๋‹จํ•˜๋Š” ๊ฒƒ

  • ์—…์บ์ŠคํŒ…: ์„œ๋ธŒ ํƒ€์ž…์˜ ๊ฐ’์„ ์Šˆํผ ํƒ€์ž…์˜ ๊ฐ’์œผ๋กœ ์ทจ๊ธ‰ํ•˜๋Š” ๊ฒƒ โœ”
  • ๋‹ค์šด์บ์ŠคํŒ…: ๋ฐ˜๋Œ€๋กœ ์Šˆํผ ํƒ€์ž…์˜ ๊ฐ’์„ ์„œ๋ธŒ ํƒ€์ž…์˜ ๊ฐ’์œผ๋กœ ์ทจ๊ธ‰ํ•˜๋Š” ๊ฒƒ โœ–

→ ์ง‘ํ•ฉ์˜ ํฌํ•จ ๊ด€๊ณ„์ฒ˜๋Ÿผ ์—…์บ์ŠคํŒ…์€ ๋ชจ๋“  ์ƒํ™ฉ์— ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ๋‹ค์šด ์บ์ŠคํŒ…์€ ๋Œ€๋ถ€๋ถ„์˜ ์ƒํ™ฉ์— ๋ถˆ๊ฐ€ํ•˜๋‹ค.

 

โœ”Unknown(์ „์ฒด ์ง‘ํ•ฉ)

๋ชจ๋“  ์„œ๋ธŒ ํƒ€์ž…์„ ๋ถ€๋ถ„์ง‘ํ•ฉ์œผ๋กœ ๊ฐ–๋Š” ์ „์ฒด ์ง‘ํ•ฉ์œผ๋กœ unknown ํƒ€์ž… ๋ณ€์ˆ˜์— ๋ชจ๋“  ํƒ€์ž…์˜ ๊ฐ’์„ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ unknown ํƒ€์ž…์˜ ๊ฐ’์€ any๋ฅผ ์ œ์™ธํ•œ ์–ด๋– ํ•œ ํƒ€์ž…์˜ ๋ณ€์ˆ˜์—๋„ ํ• ๋‹นํ•  ์ˆ˜ ์—†๋‹ค.

 

โœ”Never(๊ณต์ง‘ํ•ฉ)

never์€ ํƒ€์ž… ๊ณ„์ธต๋„์—์„œ ๊ฐ€์žฅ ์•„๋ž˜์˜ ๊ณต์ง‘ํ•ฉ์„ ์˜๋ฏธํ•œ๋‹ค. ๊ณต์ง‘ํ•ฉ์€ ์•„๋ฌด ๊ฒƒ๋„ ์—†์Œ์„ ์˜๋ฏธํ•˜๋ฉฐ never ํƒ€์ž… ๋ณ€์ˆ˜์—๋Š” ์–ด๋–ค ํƒ€์ž…์˜ ๊ฐ’๋„ ํ• ๋‹นํ•  ์ˆ˜ ์—†๋‹ค. ๋ฐ˜๋Œ€๋กœ never ํƒ€์ž…์€ ๋ชจ๋“  ํƒ€์ž…์œผ๋กœ ์—…์บ์ŠคํŒ… ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

โœ”Void

undefined๋Š” ์Šˆํผ ํƒ€์ž…์ด๋ฏ€๋กœ void๋กœ ์„ ์–ธํ•œ ํ•จ์ˆ˜์—์„œ undefined๋ฅผ ๋ฐ˜ํ™˜ํ•ด๋„ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค. voidํƒ€์ž…์—๋Š” undefined, never ํƒ€์ž…๋งŒ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค.

 

โœ”Any

ํƒ€์ž… ๊ณ„์ธต๋„๋ฅผ ์™„์ „ํžˆ ๋ฌด์‹œํ•˜๋Š” ์น˜ํŠธํ‚ค ํƒ€์ž…์ด๋‹ค. ๋ชจ๋“  ํƒ€์ž…์˜ ์Šˆํผํƒ€์ž…์ด ๋  ์ˆ˜๋„ ์žˆ๊ณ  ๋ชจ๋“  ํƒ€์ž…์˜ ์„œ๋ธŒ ํƒ€์ž…์ด ๋  ์ˆ˜๋„ ์žˆ๋‹ค.

 

โœจ ๊ฐ์ฒด ํƒ€์ž…์˜ ํ˜ธํ™˜์„ฑ

๊ฐ์ฒด๋„ ๊ฐ์ฒด๊ฐ€ ๋‹ด๋Š” ํ”„๋กœํผํ‹ฐ ๊ตฌ์กฐ์— ๋”ฐ๋ผ ์Šˆํผ~์„œ๋ธŒ ๊ด€๊ณ„๊ฐ€ ๋งŒ๋“ค์–ด์งˆ ์ˆ˜ ์žˆ๋‹ค.

type Animal = {
  name: string;
  color: string;
};

type Dog = {
  name: string;
  color: string;
  breed: string;
};

let animal: Animal = {
  name: "๊ธฐ๋ฆฐ",
  color: "yellow",
};

let dog: Dog = {
  name: "๋Œ๋Œ์ด",
  color: "brown",
  breed: "์ง„๋„",
};

animal = dog; // โœ… OK (โˆต ์„œ๋ธŒ ํƒ€์ž…์ธ dog๋ฅผ ์Šˆํผ ํƒ€์ž…์ธ animal์— ํ• ๋‹นํ•˜๋Š” ์—…์บ์ŠคํŒ…)
dog = animal; // โŒ NO (โˆต ์Šˆํผ ํƒ€์ž…์ธ animal๋ฅผ ์„œ๋ธŒ ํƒ€์ž…์ธ dog์— ํ• ๋‹นํ•˜๋Š” ๋‹ค์šด์บ์ŠคํŒ…)

 

โœ”์ดˆ๊ณผ ํ”„๋กœํผํ‹ฐ ๊ฒ€์‚ฌ

๋ณ€์ˆ˜๋ฅผ ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด๋กœ ์ดˆ๊ธฐํ™”ํ•  ๋•Œ๋Š” ํƒ€์ž…์— ์ •์˜๋œ ํ”„๋กœํผํ‹ฐ ์™ธ์˜ ๋‹ค๋ฅธ ์ดˆ๊ณผ๋œ ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ–๋Š” ๊ฐ์ฒด๋ฅผ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜์ง€ ๋ชปํ•˜๋„๋ก ์ดˆ๊ณผ ํ”„๋กœํผํ‹ฐ ๊ฒ€์‚ฌ๊ฐ€ ๋ฐœ๋™๋œ๋‹ค.

  • ๋ณ€์ˆ˜๋ฅผ ์ดˆ๊ธฐํ™”ํ•  ๋•Œ ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด์„ ์‚ฌ์šฉํ•˜์ง€๋งŒ ์•Š์œผ๋ฉด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ฆ‰, ๊ฐ’์„ ๋ณ„๋„์˜ ๋‹ค๋ฅธ ๋ณ€์ˆ˜์— ๋ณด๊ด€ํ•œ ๋‹ค์Œ ๋ณ€์ˆ˜ ๊ฐ’์„ ์ดˆ๊ธฐํ™” ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค.
interface Person {
  name: string;
  age: number;
}

const personData = {
  name: "Alice",
  age: 25,
  job: "Engineer" // ์ดˆ๊ณผ ํ”„๋กœํผํ‹ฐ ํฌํ•จ
};

const person: Person = personData; // ์˜ค๋ฅ˜ ์—†์Œ

  • ์ดˆ๊ณผ ํ”„๋กœํผํ‹ฐ ๊ฒ€์‚ฌ๋Š” ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์—๋„ ๋™์ผํ•˜๊ฒŒ ๋ฐœ์ƒํ•˜๋ฉฐ, ์ด ๋•Œ๋„ ๋ณ€์ˆ˜์— ๋ฏธ๋ฆฌ ๊ฐ’์„ ๋‹ด์•„๋‘” ๋‹ค์Œ ๋ณ€์ˆ˜๊ฐ’์„ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•˜๋ฉด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค.
interface Person {
  name: string;
  age: number;
}

function greet(person: Person) {
  console.log(`Hello, ${person.name}`);
}

// ์ดˆ๊ณผ ํ”„๋กœํผํ‹ฐ ๊ฒ€์‚ฌ ๋ฐœ์ƒ
greet({
  name: "Alice",
  age: 25,
  job: "Engineer" // ์˜ค๋ฅ˜: 'job' ํ”„๋กœํผํ‹ฐ๋Š” Person ํƒ€์ž…์— ์กด์žฌํ•˜์ง€ ์•Š์Œ
});

 

โœจ ๋Œ€์ˆ˜ ํƒ€์ž… - ํ•ฉ์ง‘ํ•ฉ, ๊ต์ง‘ํ•ฉ

โœ”ํ•ฉ์ง‘ํ•ฉ(Union)

// Union ํƒ€์ž…
let a: string | number | boolean;

a = 1;
a = "hello";
a = true;

// Union ๋ฐฐ์—ด ํƒ€์ž…
let arr: (number | string | boolean)[] = [1, "hello", true];

// Union ๊ฐ์ฒด ํƒ€์ž…
type Dog = {
  name: string;
  color: string;
};

type Person = {
  name: string;
  language: string;
};

type Union1 = Dog | Person;

 

โœ”๊ต์ง‘ํ•ฉ(Intersection)

// Intersection ํƒ€์ž…
let variable: number & string; // never ํƒ€์ž…์œผ๋กœ ์ถ”๋ก 
// ๋Œ€๋‹ค์ˆ˜์˜ ๊ธฐ๋ณธ ํƒ€์ž…๋“ค ๊ฐ„์—๋Š” ์„œ๋กœ ๊ณต์œ ํ•˜๋Š” ๊ต์ง‘ํ•ฉ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— Intersection ํƒ€์ž…์€ ๋ณดํ†ต ๊ฐ์ฒด ํƒ€์ž…์—์„œ ์‚ฌ์šฉ๋จ

// Intersection ๊ฐ์ฒด ํƒ€์ž…
type Dog = {
  name: string;
  color: string;
};

type Person = {
  name: string;
  language: string;
};

type Intersection = Dog & Person;

let intersection1: Intersection = {
  name: "",
  color: "",
  language: "",
};

 

โœจํƒ€์ž… ์ถ”๋ก 

โœ”ํƒ€์ž…์ถ”๋ก 

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์—์„œ ํƒ€์ž…์ด ์ •์˜๋˜์–ด ์žˆ์ง€ ์•Š์€ ๋ณ€์ˆ˜์˜ ํƒ€์ž…์„ ์ž๋™์œผ๋กœ ์ถ”๋ก ํ•˜๋Š” ๊ฒƒ

 

โœ”ํƒ€์ž… ์ถ”๋ก ์ด ๊ฐ€๋Šฅํ•œ ์ƒํ™ฉ๋“ค

1. ๋ณ€์ˆ˜ ์„ ์–ธ

: ์ผ๋ฐ˜์ ์ธ ๋ณ€์ˆ˜ ์„ ์–ธ์˜ ๊ฒฝ์šฐ ์ดˆ๊ธฐ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ํƒ€์ž…์ด ์ถ”๋ก ๋œ๋‹ค.(๊ฐ์ฒด ํฌํ•จ)

let age = 30; // number๋กœ ์ถ”๋ก ๋จ
let name = "Alice"; // string์œผ๋กœ ์ถ”๋ก ๋จ

let person = {
  name: "Alice",
  age: 30
}; // { name: string; age: number }๋กœ ์ถ”๋ก ๋จ

2. ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น

let person = {
  name: "Alice",
  age: 30
};

let { name, age } = person; // name์€ string, age๋Š” number๋กœ ์ถ”๋ก ๋จ

const [first, second] = [10, "hello"]; // first๋Š” number, second๋Š” string์œผ๋กœ ์ถ”๋ก ๋จ

3.ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’

function getPerson() {
  return {
    name: "Alice",
    age: 30
  };
}

const person = getPerson(); // { name: string; age: number }๋กœ ์ถ”๋ก ๋จ

4.๊ธฐ๋ณธ๊ฐ’์ด ์„ค์ •๋œ ๋งค๊ฐœ๋ณ€์ˆ˜

function greet(name = "Guest") {
  console.log(`Hello, ${name}`);
}

greet(); // name์€ string์œผ๋กœ ์ถ”๋ก ๋จ

 

โœ”์ฃผ์˜ํ•ด์•ผํ•  ์ƒํ™ฉ๋“ค

1. ์•”์‹œ์ ์œผ๋กœ any ํƒ€์ž…์œผ๋กœ ์ถ”๋ก 

์•”์‹œ์ ์œผ๋กœ ์ถ”๋ก ๋œ any ํƒ€์ž…์€ ์ฝ”๋“œ์˜ ํ๋ฆ„์— ๋”ฐ๋ผ ํƒ€์ž…์ด ๊ณ„์† ๋ณ€ํ™”ํ•œ๋‹ค. ์ด๋ฅผ any์˜ ์ง„ํ™”๋ผ๊ณ  ํ•œ๋‹ค.

let d;
// ๋ณ€์ˆ˜ ์„ ์–ธ ์‹œ ์ดˆ๊ธฐ๊ฐ’์„ ์ƒ๋žตํ•˜๋ฉด any๋กœ ์ถ”๋ก ๋œ๋‹ค.
// ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ํƒ€์ž…์ด ์•”์‹œ์  any๋กœ ์ถ”๋ก ๋  ๋•Œ์™€ ๋‹ฌ๋ฆฌ ์ผ๋ฐ˜ ๋ณ€์ˆ˜์˜ ํƒ€์ž…์ด any๋กœ ์ถ”๋ก ๋˜๋Š” ์ƒํ™ฉ์€ ์˜ค๋ฅ˜๋กœ ํŒ๋‹จํ•˜์ง€ ์•Š๋Š”๋‹ค.

d = 10; // ์•”์‹œ์  any ํƒ€์ž…์˜ ๋ณ€์ˆ˜์— ๊ฐ’์„ ํ• ๋‹นํ•˜๋ฉด
d.toFixed(); // ๋‹ค์Œ ๋ผ์ธ๋ถ€ํ„ฐ any ํƒ€์ž…์ด ํ•ด๋‹น ๊ฐ’์˜ ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™”ํ•œ๋‹ค.

d = "hello"; // ์—ฌ๊ธฐ์— ๋‹ค๋ฅธ ๊ฐ’์„ ํ• ๋‹นํ•˜๋ฉด ๋˜ ๊ทธ ๊ฐ’์— ๋งž๋Š” ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™”ํ•œ๋‹ค.
d.toUpperCase();
d.toFixed(); // ์˜ค๋ฅ˜! string ํƒ€์ž…์— number ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค ํ•จ

2. const ์ƒ์ˆ˜์˜ ์ถ”๋ก 

const num = 10;๊ณผ const str = "hello";๋Š” ๊ฐ๊ฐ number์™€ string ํƒ€์ž…์œผ๋กœ ์ถ”๋ก ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ๋ฆฌํ„ฐ๋Ÿด ํƒ€์ž…์ธ 10๊ณผ "hello"๋กœ ์ถ”๋ก ๋œ๋‹ค. ์ด ํŠน์ง•์€ TypeScript์˜ ๋ฆฌํ„ฐ๋Ÿด ํƒ€์ž… ์ถ”๋ก  ๊ธฐ๋Šฅ์œผ๋กœ, ํŠน์ • ๊ฐ’๋งŒ์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ๊ณ ์ •๋œ ํƒ€์ž…์„ ์ œ๊ณตํ•ด ์ฝ”๋“œ์˜ ํƒ€์ž… ์•ˆ์ „์„ฑ์„ ๋”์šฑ ๊ฐ•ํ™”ํ•œ๋‹ค.

const num = 10;
// 10 Number Literal ํƒ€์ž…์œผ๋กœ ์ถ”๋ก 

const str = "hello";
// "hello" String Literal ํƒ€์ž…์œผ๋กœ ์ถ”๋ก 

 

โœ”์ตœ์ ์˜ ๊ณตํ†ต ํƒ€์ž…

// ๋‹ค์–‘ํ•œ ํƒ€์ž…์˜ ์š”์†Œ๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด์„ ๋ณ€์ˆ˜์˜ ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ์„ค์ •ํ•˜๋ฉด, ์ตœ์ ์˜ ๊ณตํ†ต ํƒ€์ž…์œผ๋กœ ์ถ”๋ก ๋œ๋‹ค.

let arr = [1, "string"];
// (string | number)[] ํƒ€์ž…์œผ๋กœ ์ถ”๋ก 

 

โœจ ํƒ€์ž… ๋‹จ์–ธ

๊ฐ’ as ํƒ€์ž…์œผ๋กœ ํŠน์ • ๊ฐ’์„ ์›ํ•˜๋Š” ํƒ€์ž…์œผ๋กœ ๋‹จ์–ธํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

์šฉ๋„ → ํŠน์ • ๊ตฌ์กฐ ํƒ€์ž…์ด ์„ค์ •๋œ ๊ฐ์ฒด ๋ณ€์ˆ˜์— ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ๋นˆ ๊ฐ์ฒด๋ฅผ ๋„ฃ๊ณ  ์‹ถ์„ ๋•Œ, ์ดˆ๊ณผ ํ”„๋กœํผํ‹ฐ ๊ฒ€์‚ฌ๋ฅผ ํ”ผํ•  ๋•Œ

1. ํŠน์ • ๊ตฌ์กฐ ํƒ€์ž…์ด ์„ค์ •๋œ ๊ฐ์ฒด ๋ณ€์ˆ˜์— ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ๋นˆ ๊ฐ์ฒด๋ฅผ ๋„ฃ๊ณ  ์‹ถ์„ ๋•Œ

interface Person {
  name: string;
  age: number;
}

let person = {} as Person;
// ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ๋นˆ ๊ฐ์ฒด๋ฅผ ๋„ฃ๊ณ  ์‹ถ์ง€๋งŒ, ํƒ€์ž… ๋‹จ์–ธ์„ ํ†ตํ•ด ๋นˆ ๊ฐ์ฒด๊ฐ€ Person ํƒ€์ž…์ž„์„ ๋ช…์‹œํ•จ

// ์ดํ›„์— ํ•„์š”ํ•œ ์†์„ฑ์„ ์ถ”๊ฐ€
person.name = "Alice";
person.age = 25;

2. ์ดˆ๊ณผ ํ”„๋กœํผํ‹ฐ ๊ฒ€์‚ฌ๋ฅผ ํ”ผํ•  ๋•Œ

interface Person {
  name: string;
  age: number;
}

const person = {
  name: "Alice",
  age: 25,
  job: "Engineer"
} as Person;

// 'job' ์†์„ฑ์€ Person ํƒ€์ž…์— ์ •์˜๋˜์–ด ์žˆ์ง€ ์•Š์ง€๋งŒ, ์ดˆ๊ณผ ํ”„๋กœํผํ‹ฐ ๊ฒ€์‚ฌ๋ฅผ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด as๋ฅผ ์‚ฌ์šฉํ•จ

 

โœ”ํƒ€์ž… ๋‹จ์–ธ์˜ ์กฐ๊ฑด

๊ฐ’ as ํƒ€์ž… ํ˜•์‹์˜ ๋‹จ์–ธ์‹์„ A as B๋กœ ํ‘œํ˜„ํ–ˆ์„ ๋•Œ ์•„๋ž˜์˜ ๋‘ ์กฐ๊ฑด ์ค‘ ํ•œ ๊ฐ€์ง€๋ฅผ ๋ฐ˜๋“œ์‹œ ๋งŒ์กฑํ•ด์•ผ ํ•œ๋‹ค.

  • A๊ฐ€ B์˜ ์Šˆํผํƒ€์ž…์ด๋‹ค.
  • A๊ฐ€ B์˜ ์„œ๋ธŒํƒ€์ž…์ด๋‹ค.
let num1 = 10 as never;   // โœ…
let num2 = 10 as unknown; // โœ…

let num3 = 10 as string;  // โŒ

 

โœ”๋‹ค์ค‘ ๋‹จ์–ธ

ํƒ€์ž… ๋‹จ์–ธ์€ ๋‹ค์ค‘์œผ๋กœ ๊ฐ€๋Šฅํ•˜๊ณ , ์ด๋ฅผ ์ด์šฉํ•˜๋ฉด ์•ž์˜ ๋ถˆ๊ฐ€๋Šฅํ–ˆ๋˜ ๋‹จ์–ธ ์˜ˆ์ œ๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ด๊ฒƒ์€ ์‹ค์ œ๋กœ ๊ทธ ๊ฐ’์„ ํ•ด๋‹น ํƒ€์ž…์˜ ๊ฐ’์œผ๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋‹จ์ˆœ ๋ˆˆ์†์ž„์— ๋ถˆ๊ณผํ•˜๋ฉฐ ์˜ค๋ฅ˜ ๋ฐœ์ƒ ํ™•๋ฅ ์„ ๋†’์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ผญ ํ•„์š”ํ•œ ์ƒํ™ฉ์—์„œ๋งŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

let num3 = 10 as unknown as string;

 

 

โœ”const ๋‹จ์–ธ

ํŠน์ • ๊ฐ’์„ const ํƒ€์ž…์œผ๋กœ ๋‹จ์–ธํ•˜๋ฉด ๋งˆ์น˜ ๋ณ€์ˆ˜๋ฅผ const๋กœ ์„ ์–ธํ•œ ๊ฒƒ๊ณผ ๊ฐ™์ด ๋ฆฌํ„ฐ๋Ÿด ํƒ€์ž…์œผ๋กœ ๋ณ€๊ฒฝ๋œ๋‹ค.

let name = "Alice";
// name์˜ ํƒ€์ž…์€ string์œผ๋กœ ์ถ”๋ก ๋จ

const nameConst = "Alice";
// nameConst์˜ ํƒ€์ž…์€ "Alice"๋ผ๋Š” ๋ฆฌํ„ฐ๋Ÿด ํƒ€์ž…์œผ๋กœ ์ถ”๋ก ๋จ

let nameAsConst = "Alice" as const;
// nameAsConst์˜ ํƒ€์ž…์€ "Alice"๋ผ๋Š” ๋ฆฌํ„ฐ๋Ÿด ํƒ€์ž…์œผ๋กœ ์ถ”๋ก ๋จ

 

โœ”Not Null ๋‹จ์–ธ

๊ฐ’ as ํƒ€์ž…์„ ๋”ฐ๋ฅด์ง€ ์•Š๋Š” ๋‹จ์–ธ์œผ๋กœ, ๊ฐ’ ๋’ค์— ๋Š๋‚Œํ‘œ(!)๋ฅผ ๋ถ™์—ฌ์ฃผ๋ฉด ์ด ๊ฐ’์ด undefined์ด๊ฑฐ๋‚˜ null์ด ์•„๋‹ ๊ฒƒ์œผ๋กœ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ๋‹ค.

type Post = {
  title: string;
  author?: string;
};

let post: Post = {
  title: "๊ฒŒ์‹œ๊ธ€1",
};

const len: number = post.author.length; // ์˜ค๋ฅ˜! post.author๊ฐ€ undefined๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ์—ฌ๊ฒจ์ง„๋‹ค.

const len: number = post.author!.length;
// !๋ฅผ ๋ถ™์—ฌ post.author๊ฐ€ non null ์ž„์„ ๋‹จ์–ธํ•ด์„œ ์˜ค๋ฅ˜๋ฅผ ํ•ด๊ฒฐํ•œ๋‹ค.

 

โœจํƒ€์ž… ์ขํžˆ๊ธฐ

์กฐ๊ฑด๋ฌธ์„ ์ด์šฉํ•ด ์กฐ๊ฑด๋ฌธ ๋‚ด๋ถ€์—์„œ ๋ณ€์ˆ˜๊ฐ€ ํŠน์ • ํƒ€์ž…์ž„์„ ๋ณด์žฅํ•˜๋ฉด ํ•ด๋‹น ์กฐ๊ฑด๋ฌธ ๋‚ด๋ถ€์—์„œ๋Š” ๋ณ€์ˆ˜์˜ ํƒ€์ž…์ด ๋ณด์žฅ๋œ ํƒ€์ž…์œผ๋กœ ์ขํ˜€์ง€๋Š” ๊ฒƒ

if(typeof === …) ์ฒ˜๋Ÿผ ์กฐ๊ฑด๋ฌธ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•ด ํƒ€์ž…์„ ์ขํžˆ๋Š” ์ด๋Ÿฐ ํ‘œํ˜„๋“ค์„ “ํƒ€์ž… ๊ฐ€๋“œ”๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

 

โœ”instanceof ํƒ€์ž…๊ฐ€๋“œ

instanceof๋Š” number๋‚˜ string๊ณผ ๊ฐ™์€ ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•์—๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. instanceof ์—ฐ์‚ฐ์ž๋Š” ๊ฐ์ฒด๊ฐ€ ํŠน์ • ํด๋ž˜์Šค๋‚˜ ์ƒ์„ฑ์ž์˜ ์ธ์Šคํ„ด์Šค์ธ์ง€ ํ™•์ธํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋ฉฐ, ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•์ด ์•„๋‹Œ ๊ฐ์ฒด ํƒ€์ž…์—๋งŒ ์ ์šฉ๋œ๋‹ค.

// ๋‚ด์žฅ ํด๋ž˜์Šค ํƒ€์ž…(์•„๋ž˜ ์˜ˆ์ œ์˜ Date)์— ์‚ฌ์šฉ ๊ฐ€๋Šฅ
function func(value: number | string | Date | null) {
  if (typeof value === "number") {
    console.log(value.toFixed());
  } else if (typeof value === "string") {
    console.log(value.toUpperCase());
  } else if (value instanceof Date) {
    console.log(value.getTime());
  }
}

 

โœ”in ํƒ€์ž…๊ฐ€๋“œ

in ํƒ€์ž…๊ฐ€๋“œ๋Š” TypeScript์—์„œ ๊ฐ์ฒด์— ํŠน์ • ํ”„๋กœํผํ‹ฐ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ๊ฒ€์‚ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ์ด๋ฅผ ํ†ตํ•ด TypeScript๋Š” ํ•ด๋‹น ํ”„๋กœํผํ‹ฐ๊ฐ€ ์กด์žฌํ•  ๊ฒฝ์šฐ, ํ•ด๋‹น ํ”„๋กœํผํ‹ฐ ํƒ€์ž…์„ ์•ˆ์ „ํ•˜๊ฒŒ ์ถ”๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค. in ์—ฐ์‚ฐ์ž๋Š” ์ฃผ๋กœ ์œ ๋‹ˆ์˜จ ํƒ€์ž…์—์„œ ํŠน์ • ํ”„๋กœํผํ‹ฐ๊ฐ€ ํฌํ•จ๋œ ํƒ€์ž…์„ ๊ตฌ๋ถ„ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.

interface Dog {
  bark: () => void;
}

interface Cat {
  meow: () => void;
}

function makeSound(animal: Dog | Cat) {
  if ('bark' in animal) {
    animal.bark(); // TypeScript๋Š” animal์ด Dog ํƒ€์ž…์ž„์„ ์ถ”๋ก 
  } else {
    animal.meow(); // TypeScript๋Š” animal์ด Cat ํƒ€์ž…์ž„์„ ์ถ”๋ก 
  }
}

 

โœจ ์„œ๋กœ์†Œ ์œ ๋‹ˆ์˜จ ํƒ€์ž…

“์„œ๋กœ์†Œ ์œ ๋‹ˆ์˜จ ํƒ€์ž…”์ด๋ž€ ๊ต์ง‘ํ•ฉ์ด ์—†๋Š” ํƒ€์ž…๋“ค ์ฆ‰ ์„œ๋กœ์†Œ ๊ด€๊ณ„์— ์žˆ๋Š” ํƒ€์ž…๋“ค์„ ๋ชจ์•„ ๋งŒ๋“  ์œ ๋‹ˆ์˜จ ํƒ€์ž…์ด๋‹ค.

type Admin = {
  name: string;
  kickCount: number;
};

type Member = {
  name: string;
  point: number;
};

type Guest = {
  name: string;
  visitCount: number;
};

type User = Admin | Member | Guest;

function login(user: User) {
  if ("kickCount" in user) {
		// Admin
    console.log(`${user.name}๋‹˜ ํ˜„์žฌ๊นŒ์ง€ ${user.kickCount}๋ช… ์ถ”๋ฐฉํ–ˆ์Šต๋‹ˆ๋‹ค`);
  } else if ("point" in user) {
		// Member
    console.log(`${user.name}๋‹˜ ํ˜„์žฌ๊นŒ์ง€ ${user.point}๋ชจ์•˜์Šต๋‹ˆ๋‹ค`);
  } else {
		// Guest
    console.log(`${user.name}๋‹˜ ํ˜„์žฌ๊นŒ์ง€ ${user.visitCount}๋ฒˆ ์˜ค์…จ์Šต๋‹ˆ๋‹ค`);
  }
}

์œ„์™€ ๊ฐ™์€ ํšŒ์› ๊ด€๋ฆฌ ํ”„๋กœ๊ทธ๋žจ์—์„œ 3๊ฐœ์˜ ํƒ€์ž…์„ ๊ตฌ๋ณ„ํ•˜๊ธฐ ์œ„ํ•œ ์กฐ๊ฑด์‹์„ ์ง๊ด€์ ์ด์ง€ ๋ชปํ•˜๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ ํƒ€์ž…์— ํƒœ๊ทธ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

type Admin = {
  tag: "ADMIN";
  name: string;
  kickCount: number;
};

type Member = {
  tag: "MEMBER";
  name: string;
  point: number;
};

type Guest = {
  tag: "GUEST";
  name: string;
  visitCount: number;
};

// ํƒœ๊ทธ ํ”„๋กœํผํ‹ฐ๋ฅผ ํ†ตํ•ด 3๊ฐœ์˜ ์„œ๋กœ์†Œ ์œ ๋‹ˆ์˜จ ํƒ€์ž…์„ ๋” ์ง๊ด€์ ์œผ๋กœ ๊ตฌ๋ณ„ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.
function login(user: User) {
  if (user.tag === "ADMIN") {
    console.log(`${user.name}๋‹˜ ํ˜„์žฌ๊นŒ์ง€ ${user.kickCount}๋ช… ์ถ”๋ฐฉํ–ˆ์Šต๋‹ˆ๋‹ค`);
  } else if (user.tag === "MEMBER") {
    console.log(`${user.name}๋‹˜ ํ˜„์žฌ๊นŒ์ง€ ${user.point}๋ชจ์•˜์Šต๋‹ˆ๋‹ค`);
  } else {
    console.log(`${user.name}๋‹˜ ํ˜„์žฌ๊นŒ์ง€ ${user.visitCount}๋ฒˆ ์˜ค์…จ์Šต๋‹ˆ๋‹ค`);
  }
}

// switch๋ฅผ ์ด์šฉํ•œ ๋” ์ง๊ด€์ ์ธ ์กฐ๊ฑด ๋ถ„๋ฅ˜๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.
function login(user: User) {
  switch (user.tag) {
    case "ADMIN": {
      console.log(`${user.name}๋‹˜ ํ˜„์žฌ๊นŒ์ง€ ${user.kickCount}๋ช… ์ถ”๋ฐฉํ–ˆ์Šต๋‹ˆ๋‹ค`);
      break;
    }
    case "MEMBER": {
      console.log(`${user.name}๋‹˜ ํ˜„์žฌ๊นŒ์ง€ ${user.point}๋ชจ์•˜์Šต๋‹ˆ๋‹ค`);
      break;
    }
    case "GUEST": {
      console.log(`${user.name}๋‹˜ ํ˜„์žฌ๊นŒ์ง€ ${user.visitCount}๋ฒˆ ์˜ค์…จ์Šต๋‹ˆ๋‹ค`);
      break;
    }
  }
}
728x90