Interface
๊ฐ์ฒด์ ๊ตฌ์กฐ/ํํ๋ฅผ ์ ์ํ๋ฉฐ, ํน์ ๊ฐ์ฒด๊ฐ ์ด๋ค Property, Method๋ฅผ ๊ฐ์ ธ์ผ ํ๋์ง๋ฅผ ๋ํ๋ธ๋ค.
์ฝ๋์ ๊ฐ๋ ์ฑ๊ณผ ์์ ์ฑ์ ๋์ผ ์ ์์ผ๋ฉฐ, ํ์ฅ ๋ฐ ์กฐํฉ๋ ์ฉ์ดํ๋ค.
interface ์ด๋ฆ {
์์ฑ ์ด๋ฆ: ์์ฑ ํ์
;
์์ฑ ์ด๋ฆ: ์์ฑ ํ์
;
๋ฉ์๋ ์ด๋ฆ(): ๋ฉ์๋ ํ์
;
}
์์
interface User {
id: number;
name: string;
isPremium: boolean;
someMethod(): void;
}
const userA: User = {
id: 10,
name: "Bill",
isPremium: false,
someMethod() {
console.log("some method called")
}
}
alias vs interface
์จ๋ฆฌ์ด์ค์ ์ธํฐํ์ด์ค์ ๋ชฉ์ ์ ํฌ๊ฒ ๋ค๋ฅด์ง ์์ง๋ง, ํ์ฅ ๋ฌธ๋ฒ์ด ๋ค๋ฅด๋ค.
- interface(์ธํฐํ์ด์ค)
- interface User{} ๊ฐ์ด ํํ
- ๊ฐ์ฒด ํ์ (ํํ)๋ก ์ฌ์ฉํด์ผ ํจ
interface Person {
name: string;
}
interface Employee extends Person {
salary: number;
}
const employee: Employee = {
name: "Alice",
salary: 50000,
};
- type Alias(ํ์ ๋ณ์นญ)
- type User = {} ๊ฐ์ด ํํ
- ๊ฐ์ฒด ํฌํจ, ๋ฆฌํฐ๋ด, ์์๊ฐ ๋ฑ์ ํ์ ์ผ๋ก ๊ฐ์ง ์ ์์
type Person = {
name: string;
};
type Employee = Person & {
salary: number;
};
const employee: Employee = {
name: "Alice",
salary: 50000,
};
readonly & optional property
์ฝ๊ธฐ ์ ์ฉ property๋ readonly ํค์๋๋ฅผ ์ฌ์ฉํด ์ ์ํ๊ณ , ์ ํ์ property๋ ? ๋ฅผ ์ฌ์ฉํด ํ์ํ๋ค.
interface User {
readonly id:number;
name:string;
isPremium?:boolean;
}
const testUser:User = {
id:100,
name:"John"
}
// testUser.id = 200 // readonly property๋ผ์ ์์ ๋ถ๊ฐ
๋ฉ์๋ / ํจ์ ํ์
interface User {
readonly id: number;
name: string;
isPremium?: boolean;
greet(): void;
}
const testUser: User = {
id: 100,
name: "John",
isPremium: false,
greet() {
console.log(`Hello ${this.name}`)
}
}
testUser.greet()
๋ง์ฝ ์ฌ๊ธฐ์ ๋ฉ์๋ greet()๊ฐ ๋ฐํ๊ฐ์ด ์๋ค๋ฉด return ๊ฐ์ผ๋ก ๋์ฒดํด์ฃผ๋ฉด ๋๋ค.
interface User {
readonly id: number;
name: string;
isPremium?: boolean;
greet(): string;
}
const testUser: User = {
id: 100,
name: "John",
isPremium: false,
greet() {
return `Hello ${this.name}`
}
}
testUser.greet()
์ด๋ greet()์ ํ๋ผ๋ฏธํฐ๋ฅผ ์ถ๊ฐํ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ํด์ฃผ๋ฉด ๋๋ค.
interface User {
readonly id: number;
name: string;
isPremium?: boolean;
greet(message: string): string;
}
const testUser: User = {
id: 100,
name: "John",
isPremium: false,
greet(message: string) {
return `${message}, ${this.name}`
}
}
testUser.greet("Hi");
ํ์ฅ ๋ฐ ์กฐํฉ
interface Person {
name: string;
age: number;
}
interface Employee{
employeeId: number;
}
interface Student extends Person, Employee {
// name, age ๊ทธ๋ฆฌ๊ณ studentId ํ๋กํผํฐ๊น์ง ๊ฐ์ง๊ฒ๋จ
studentId: number;
}
const personA: Person = {
name: "Maki",
age: 20
}
const studentA: Student = {
name: "Harua",
age: 21,
studentId: 123124,
employeeId: 62389
}
ํํ(Tuples)
๊ณ ์ ๋ ํฌ๊ธฐ์ ๋ฐฐ์ด๋ก, ๊ณ ์ ๋ ์์ ์์๋ฅผ ๊ฐ์ง๊ณ ๊ฐ ์์์ ํ์ ์ด ๋ฏธ๋ฆฌ ์ ํด์ ธ์๋ค.
์๋ฐ์คํฌ๋ฆฝํธ์๋ ์๋ ๊ฐ๋ ์ด๋ค.
// ๊ธฐ๋ณธ ๋ฌธ๋ฒ
let myTuple: [string, number, boolean]
myTuple = ["Hello", 15, false]
myTuple = ["Hello", true, 1] // ์์๊ฐ ์ผ์นํ์ง ์์
myTuple = ["Hello", 1] // ๊ฐ์๊ฐ ์ผ์นํ์ง ์์
์์
//#1. ํจ์๊ฐ ์ฌ๋ฌ ํ์
์ ๋ฐํํ ๋
function getUserInfo(): [number, string] {
return [1, "Sam"]
}
const [userId, username] = getUserInfo()
//#2. ํ์
์จ๋ฆฌ์ด์ค๋ฅผ ์ฌ์ฉํ ๋
type Flavor = string;
type Price = number;
type IceCream = [Flavor, Price]
const greenTea: IceCream = ["greenTea", 500]
greenTea[0]
greenTea[1]
//#3. ํํ์ ๋ด๋ถ์์๋ก ์ฌ์ฉํ ๋
type lat = number;
type lng = number;
type coord = [lat, lng]
let coords: coord[] = []; // ๋น ๋ฐฐ์ด๋ก ์ด๊ธฐํ
coords.push([36, -95])
coords.push([38, -74])
// coords.push([false, "1"]) // ๋น์ฐํ ์ค๋ฅ..
// ๊ตฌ์กฐ๋ถํด ํ ๋น 1
for (const [lat, lng] of coords) {
console.log(lat, lng)
}
// ๊ตฌ์กฐ๋ถํด ํ ๋น 2
let exampleTuple: [string, number] = ["hello", 42];
let [greeting, answer] = exampleTuple;
console.log(greeting); // "hello"
console.log(answer); // 42
์ด๊ฑฐํ(Enum)
์์์ ๋ช ๋ช ๋ ์งํฉ์ ์ฝ๊ฒ ๋ค๋ฃจ๊ธฐ ์ํด ์ฌ์ฉ๋๋ค.
์ฌ๋ฌ ์์ ๊ฐ๋ค์ ๊ทธ๋ฃนํํ๋ ๊ฒ์ด ๋ชฉ์ ์ด๋ฉฐ, ์ญ์ ์๋ฐ์คํฌ๋ฆฝํธ์๋ ์กด์ฌํ์ง ์๋ ๊ฐ๋ ์ด๋ค.
enum ์ด๋ฆ {
// ๊ฐ ๋ฉค๋ฒ๊ฐ ํ ๋น๊ฐ์ ๊ฐ์ง๊ณ ์์
์์๊ฐ1, // ์๋์ผ๋ก 0์ด๋ผ๋ ๊ฐ์ ๊ฐ์ง๊ณ ์์
์์๊ฐ2, // 1
์์๊ฐ3, // 2
์์๊ฐ4, // 3
}
// ๊ฐ์ ๋ช
์์ ์ผ๋ก ์ง์ ํ ๊ฒฝ์ฐ
enum ์ด๋ฆ1 {
์์๊ฐ1 = 1,
์์๊ฐ2, // 2
์์๊ฐ3, // 3
์์๊ฐ4, // 4
}
์ซ์ํ Enum
enum PlayerState {
Buffering, // 0
Playing, // 1
Paused, // 2
Seeking // 3
}
let currentState: PlayerState;
currentState = PlayerState.Buffering
currentState = PlayerState.Playing
// currentState = "Playing" // ๋ณ์๊ฐ ๊ฐ์ง ์ ์๋ ๊ฐ์ Enum์
const isPlaying = (state:PlayerState) => {
return state === PlayerState.Playing
}
// ํ๋ ์ด์ด ์ฌ์์ค?
isPlaying(currentState); // true
๋ฌธ์์ด Enum
enum Direction {
Left = "LEFT",
Right = "RIGHT",
Up = "UP",
Down = "DOWN"
}
function move(dir: Direction) {
switch (dir) {
case Direction.Left:
// ์ผ์ชฝ ๋ก์ง
break;
case Direction.Right:
// ์ค๋ฅธ์ชฝ ๋ก์ง
break;
// ...
}
}
move(Direction.Left)
move(Direction.Right)
'typescript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
TypeScript / ์ ๋ค๋ฆญ (2) | 2025.06.05 |
---|---|
TypeScript / ํด๋์ค (0) | 2025.06.05 |
TypeScript / ํ์ ์ถ๋ก , ํ์ ๋จ์ธ (0) | 2025.06.05 |
TypeScript / ํ์ (1) (0) | 2025.06.04 |
TypeScript / ๊ฐ์ (0) | 2025.06.02 |