๐ 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๋ ๊ฐ์ฒด, ํจ์, ํด๋์ค ๋ฑ ๋ค์ํ ๊ตฌ์กฐ๋ฅผ ๋ช ํํ๊ฒ ์ ์ํ์ฌ ํ์ ์์ ์ฑ์ ๋์ฌ์ค๋๋ค. ๊ฐ๋ฐ์ ๊ฐ ํ์ ์ ์ฝ๋์ ์๋๋ฅผ ๋ช ํํ ์ ๋ฌํ ์ ์๊ณ , ์ปดํ์ผ ๋จ๊ณ์์ ๋ง์ ์ค๋ฅ๋ฅผ ์ก์์ค ์ ์์ด์.
๐ท์ ์ค์ ๊ฐ๋ฐ์๊ฐ ๋์ด๋ด ์๋น! ๐ท