๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

typescript

TypeScript / ํƒ€์ž…(2)

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