[TypeScript] 인터페이스 (Interface)

2024. 11. 8. 01:04·TypeScript
728x90
728x90

인터페이스 (Interface)

  - 객체의 구조를 정의하는 문법

  - 객체의 형태를 사전에 정의함으로써, 예상치 못한 에러를 방지할 수 있음

 

# 객체 구조 정의

interface Person {
    name: string;
    age?: number; // ?를 붙이면 옵션 속성이 됨
}

--------------------

// 정상 작동
const person: Person = {
    name: "John",
    age: 30
};

// gender 속성은 정의되어 있지 않기 때문에 에러 발생
const person: Person = {
    name: "John",
    age: 30,
    gender: 'M'
};

 

# 함수 파라미터 타입 정의

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

// string 타입의 name과 number 타입의 age 속성을 갖는 객체만 인자로 받을 수 있음
function getName(person: Person) {
	return person.name;
}

 

# 함수 반환 타입 정의

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

// Person 타입의 객체를 반환하지 않으면 에러 발생
function getPerson(person: Person): Person {
    return person;
}

 


인터페이스 상속

  - 인터페이스는 다른 인터페이스를 상속받아 확장할 수 있음

     → 인터페이스 간의 중복 감소

  - 반드시 부모 인터페이스에 정의된 속성들을 자식 인터페이스에서 모두 보장해주어야 함

     ex) 부모 인터페이스에서 name 속성을 string으로 정의했을 때,

           자식 인터페이스가 name 속성을 number로 재정의하려고 하면 에러 발생

interface Animal {
    name: string;
}

interface Dog extends Animal {
    breed: string;
}

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

 

# 계층적으로 확장 가능

interface Animal {
    hasLife: boolean;
}

interface Mammal extends Animal {
    hasFur: boolean;
}

interface Human extends Mammal {
    name: string;
    age: number;
}

const person: Human = {
    hasLife: true,  // Animal에서 상속된 속성
    hasFur: false,  // Mammal에서 상속된 속성
    name: "Alice",  // Human에서 추가된 속성
    age: 30         // Human에서 추가된 속성
};

 

# 다중 상속 가능

interface Flyer {
    fly(): void;
}

interface Swimmer {
    swim(): void;
}

interface FlyingFish extends Flyer, Swimmer {}

const flyingFish: FlyingFish = {
    fly() {
        console.log("Flying through the air!");
    },
    swim() {
        console.log("Swimming in the water!");
    }
};

 

 

 

320x100
반응형
저작자표시 비영리 변경금지 (새창열림)

'TypeScript' 카테고리의 다른 글

[TypeScript] 타입 추론 (Type Inference) & 타입 단언 (Type Assertion)  (0) 2024.11.12
[TypeScript] 타입 별칭 (Type Alias)  (0) 2024.11.10
[TypeScript] 유니언 타입 & 인터섹션 타입  (0) 2024.11.09
[TypeScript] 기본 타입  (4) 2024.11.07
[TypeScript] 개요  (3) 2024.11.07
'TypeScript' 카테고리의 다른 글
  • [TypeScript] 타입 별칭 (Type Alias)
  • [TypeScript] 유니언 타입 & 인터섹션 타입
  • [TypeScript] 기본 타입
  • [TypeScript] 개요
스응
스응
    반응형
    250x250
  • 스응
    이서영의 개발 블로그
    스응
  • 전체
    오늘
    어제
  • 글쓰기 관리
    • 분류 전체보기 (385)
      • Java (134)
        • Base (54)
        • Spring Boot (37)
        • JSP (16)
        • Swing (GUI) (20)
        • Design Pattern (7)
      • C# (13)
      • PHP (18)
      • SQL (27)
      • Vue.js (9)
      • Tailwind CSS (4)
      • TypeScript (7)
      • HTML & CSS (27)
      • JavaScript (26)
      • jQuery (10)
      • Android (3)
      • - - - - - - - - - - - - - - (0)
      • Hotkeys (5)
      • CS (30)
      • IT Notes (13)
      • Error Notes (17)
      • Team Project (24)
        • Airlines Web Project (12)
        • University Web Project (6)
        • Strikers 1945 GUI Project (6)
      • My Project (18)
        • Library Web Project (8)
        • Pet Shopping Mall GUI Project (10)
      • etc. (0)
  • 블로그 메뉴

    • Home
    • Write
  • 링크

    • 깃허브
  • 공지사항

  • 인기 글

  • 태그

    git
    vuejs
    http
    js
    Android
    Codeigniter
    HTML
    SWAGGER
    java
    C#
    tailwindcss
    jsp
    CSS
    개발일지
    php
    오블완
    SQL
    Hotkeys
    Swing
    티스토리챌린지
    cs
    typeScript
    면접
    Wordpress
    SpringBoot
    SEO
    zapier
    jQuery
    errorNote
  • 최근 댓글

  • hELLO· Designed By정상우.v4.10.0
스응
[TypeScript] 인터페이스 (Interface)
상단으로

티스토리툴바