Front-end/Typescript

[TypeScript] Interface: ํƒ€์ž…์„ ๊ตฌ์กฐ์ ์œผ๋กœ ์ •์˜ํ•˜๊ธฐ!๐Ÿ“˜

xeunnie 2024. 12. 26. 01:00
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ“˜ TypeScript์˜ Interface ํ†บ์•„๋ณด๊ธฐ!

TypeScript์—์„œ interface๋Š” ๊ฐ์ฒด์˜ ๊ตฌ์กฐ๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ํ•จ์ˆ˜, ํด๋ž˜์Šค, ๊ฐ์ฒด ๋“ฑ ๋‹ค์–‘ํ•œ ํƒ€์ž…์„ ์•ˆ์ „ํ•˜๊ณ  ๋ช…ํ™•ํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ์ค‘์š”ํ•œ ๋„๊ตฌ์ฃ . ์ด ๊ธ€์—์„œ๋Š” interface์— ๋Œ€ํ•ด ๊นŠ์ด ์žˆ๊ฒŒ ๋‹ค๋ฃจ์–ด ๋ณผ๊ฒŒ์š”. ๐Ÿ› ๏ธ


๐Ÿ” Interface๋ž€ ๋ฌด์—‡์ธ๊ฐ€์š”?

Interface๋Š” ๊ฐ์ฒด์˜ ํƒ€์ž…์„ ์„ ์–ธํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” TypeScript์˜ ํ‚ค์›Œ๋“œ์ž…๋‹ˆ๋‹ค. ๊ฐ์ฒด๊ฐ€ ๊ฐ€์ ธ์•ผ ํ•  ์†์„ฑ๊ณผ ์†์„ฑ์˜ ํƒ€์ž…์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์–ด์š”.

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

const person: Person = {
  name: "John",
  age: 30,
};

 

์œ„ ์˜ˆ์ œ์—์„œ Person์€ name๊ณผ age ์†์„ฑ์„ ๊ฐ€์ง„ ๊ฐ์ฒด ํƒ€์ž…์„ ์ •์˜ํ•˜๋Š” interface์ž…๋‹ˆ๋‹ค. person ๊ฐ์ฒด๋Š” Person ํƒ€์ž…์„ ๋”ฐ๋ฅด๋ฉฐ, ์ง€์ •๋œ ์†์„ฑ ๋ฐ ํƒ€์ž…์— ๋งž์ถฐ ์ž‘์„ฑ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.


๐Ÿ› ๏ธ Interface ๊ธฐ๋ณธ ๋ฌธ๋ฒ•

1. ํ•„์ˆ˜ ์†์„ฑ๊ณผ ์„ ํƒ์  ์†์„ฑ

  • ํ•„์ˆ˜ ์†์„ฑ: ๋ฐ˜๋“œ์‹œ ํฌํ•จํ•ด์•ผ ํ•˜๋Š” ์†์„ฑ์ž…๋‹ˆ๋‹ค.
  • ์„ ํƒ์  ์†์„ฑ: **?**๋ฅผ ์‚ฌ์šฉํ•ด ์„ ํƒ์ ์œผ๋กœ ํฌํ•จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
interface Car {
  make: string; // ํ•„์ˆ˜ ์†์„ฑ
  model: string; // ํ•„์ˆ˜ ์†์„ฑ
  year?: number; // ์„ ํƒ์  ์†์„ฑ
}

const myCar: Car = {
  make: "Tesla",
  model: "Model 3",
};

const oldCar: Car = {
  make: "Toyota",
  model: "Corolla",
  year: 2005,
};

 

2. ์ฝ๊ธฐ ์ „์šฉ(Readonly) ์†์„ฑ

์†์„ฑ์„ ์ฝ๊ธฐ ์ „์šฉ์œผ๋กœ ์„ค์ •ํ•˜๋ฉด ์ˆ˜์ •ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ํ‚ค์›Œ๋“œ readonly๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

interface Book {
  readonly title: string;
  readonly author: string;
}

const book: Book = {
  title: "1984",
  author: "George Orwell",
};

// book.title = "Animal Farm"; // ์˜ค๋ฅ˜ ๋ฐœ์ƒ: ์ฝ๊ธฐ ์ „์šฉ ์†์„ฑ์€ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

 

3. ์ธ๋ฑ์Šค ์‹œ๊ทธ๋‹ˆ์ฒ˜(Index Signature)

๋™์ ์œผ๋กœ ํ‚ค์™€ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

interface StringDictionary {
  [key: string]: string; // ํ‚ค๋Š” ๋ฌธ์ž์—ด, ๊ฐ’๋„ ๋ฌธ์ž์—ด
}

const translations: StringDictionary = {
  hello: "์•ˆ๋…•ํ•˜์„ธ์š”",
  world: "์„ธ๊ณ„",
};

console.log(translations["hello"]); // "์•ˆ๋…•ํ•˜์„ธ์š”"

 

4. ํ•จ์ˆ˜ ํƒ€์ž…(Function Types)

interface๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•จ์ˆ˜์˜ ํƒ€์ž…๋„ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

interface Add {
  (a: number, b: number): number; // ์ž…๋ ฅ๊ณผ ๋ฐ˜ํ™˜๊ฐ’ ํƒ€์ž… ์ •์˜
}

const add: Add = (a, b) => a + b;

console.log(add(5, 3)); // 8

 

5. ํ™•์žฅ(Extends)

interface๋Š” ๋‹ค๋ฅธ interface๋ฅผ ํ™•์žฅ(Extend)ํ•˜์—ฌ ๊ธฐ์กด ์†์„ฑ์„ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

interface Animal {
  name: string;
}

interface Dog extends Animal {
  breed: string;
}

const myDog: Dog = {
  name: "Buddy",
  breed: "Golden Retriever",
};

 

6. ํ˜ผํ•ฉ ํƒ€์ž…(Hybrid Types)

interface๋Š” ๊ฐ์ฒด์™€ ํ•จ์ˆ˜๊ฐ€ ํ˜ผํ•ฉ๋œ ํƒ€์ž…๋„ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

interface Counter {
  (start: number): string;
  increment(): void;
  reset(): void;
}

function getCounter(): Counter {
  const counter = ((start: number) => `Counter started at ${start}`) as Counter;
  counter.increment = () => console.log("Incremented!");
  counter.reset = () => console.log("Reset!");
  return counter;
}

const counter = getCounter();

console.log(counter(10)); // "Counter started at 10"
counter.increment(); // "Incremented!"
counter.reset(); // "Reset!"

 

7. ํด๋ž˜์Šค์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๊ธฐ

interface๋Š” ํด๋ž˜์Šค์—์„œ ๊ตฌํ˜„ํ•ด์•ผ ํ•  ๊ตฌ์กฐ๋ฅผ ๊ฐ•์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

interface Shape {
  area(): number;
}

class Rectangle implements Shape {
  constructor(private width: number, private height: number) {}
  area(): number {
    return this.width * this.height;
  }
}

const rect = new Rectangle(5, 10);

console.log(rect.area()); // 50

๐Ÿ›‘ Interface์™€ Type์˜ ์ฐจ์ด

Interface

  • ๊ฐ์ฒด์˜ ๊ตฌ์กฐ๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐ ์ตœ์ ํ™”๋จ
  • ํ™•์žฅ(extends) ๊ฐ€๋Šฅ
  • ์ปดํŒŒ์ผ ํƒ€์ž„์—๋งŒ ์กด์žฌ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ์ปดํŒŒ์ผ๋˜๋ฉด ์‚ฌ๋ผ์ง

Type

  • ์œ ๋‹ˆ์˜จ(|) ๋ฐ ์ธํ„ฐ์„น์…˜(&) ๊ฐ™์€ ๋” ๋ณต์žกํ•œ ํƒ€์ž… ์ •์˜ ๊ฐ€๋Šฅ
  • ๊ธฐ๋ณธ ํƒ€์ž…(์˜ˆ: string, number)๋„ alias๋กœ ์ •์˜ ๊ฐ€๋Šฅ
  • ์ธํ„ฐํŽ˜์ด์Šค์™€ ๋‹ฌ๋ฆฌ ์žฌ์„ ์–ธ ๋ถˆ๊ฐ€๋Šฅ

๐ŸŒŸ ์‹ค๋ฌด์—์„œ ์ž์ฃผ ์“ฐ์ด๋Š” Interface ํŒจํ„ด

1. API ์‘๋‹ต ํƒ€์ž… ์ •์˜

interface ApiResponse<T> {
  data: T;
  success: boolean;
  message: string;
}

interface User {
  id: number;
  name: string;
}

const response: ApiResponse<User> = {
  data: { id: 1, name: "Alice" },
  success: true,
  message: "User fetched successfully",
};

 

2. ์ปดํฌ๋„ŒํŠธ Props ์ •์˜

React ์ปดํฌ๋„ŒํŠธ์—์„œ props ํƒ€์ž…์„ ์ •์˜ํ•  ๋•Œ interface๋ฅผ ์ž์ฃผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

interface ButtonProps {
  label: string;
  onClick: () => void;
}

function Button({ label, onClick }: ButtonProps) {
  return <button onClick={onClick}>{label}</button>;
}

๐Ÿ”ฅ Interface๋กœ ์ฝ”๋“œ๋ฅผ ๋” ํƒ€์ž… ์•ˆ์ „ํ•˜๊ฒŒ!

TypeScript์˜ interface๋Š” ๊ฐ์ฒด, ํ•จ์ˆ˜, ํด๋ž˜์Šค ๋“ฑ ๋‹ค์–‘ํ•œ ๊ตฌ์กฐ๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ์ •์˜ํ•˜์—ฌ ํƒ€์ž… ์•ˆ์ •์„ฑ์„ ๋†’์—ฌ์ค๋‹ˆ๋‹ค. ๊ฐœ๋ฐœ์ž ๊ฐ„ ํ˜‘์—… ์‹œ ์ฝ”๋“œ์˜ ์˜๋„๋ฅผ ๋ช…ํ™•ํžˆ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๊ณ , ์ปดํŒŒ์ผ ๋‹จ๊ณ„์—์„œ ๋งŽ์€ ์˜ค๋ฅ˜๋ฅผ ์žก์•„์ค„ ์ˆ˜ ์žˆ์–ด์š”.

 

๐ŸŒท์ „์„ค์˜ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜์–ด๋ด…์‹œ๋‹น! ๐ŸŒท

728x90
๋ฐ˜์‘ํ˜•