
interface 문법
interface Square {
color :string,
width :number,
}
let 네모 :Square = { color : 'red', width : 100 }
type 문법이랑 크게 다를 것은 없다.
차이점을 설명하자면
1. 중복선언이 가능하다
interface Student {name : string}
interface Student {score : number}
Student라는 인터페이스 안에는 name과 score 변수가 둘 다 존재한다.
extends 키워드 사용 전
interface Student {
name :string,
}
interface Teacher {
name :string,
age :number
}
다른 인터페이스이지만 중복되는 변수가 존재한다면 어딘가 불편함이 생긴다.
extends 키워드 사용 후
interface Student {
name :string,
}
interface Teacher extends Student {
age :number
}
extends 키워드 사용.
=> 부모에게 상속을 받아 중복되는 변수를 따로 선언할 필요가 없다.
편리함
그럼 type 키워드는 상속개념이 없나?
type Animal = {
name :string
}
type Cat = Animal & { age: number }
상속개념과는 다르지만 &연산자를 이용한다면 비슷한 원리로 작동할 수 있다.
1. interface도 &연산자를 사용할 수 있다.
2. &연산자는 intersection이라고 칭하며 교차해달라는 의미이다.
=> 즉 왼쪽타입과 오른쪽타입을 전부 만족하는 타입을 생성한다는 개념이라고 생각하면 이해하기 편하다.
조금 다르긴 하지만,
interface는 java공부할 때 많이 익숙한 친구라 쉽게 다가갈 수 있었다.
엄격함을 기준으로 봤을 때는 중복성 제한이 있는 type이 타입스크립트에서는 더 맞는 표현 같지만
interface는 추후에 타입을 더하거나 다른 사람의 코드를 수정할 일이 생긴다면 편리하게 애용할 것으로 예상된다.
'TypeScript' 카테고리의 다른 글
Narrowing 추가적인 방법들 (0) | 2023.07.03 |
---|---|
rest parameter , spread operator, destructuring 타입지정 (0) | 2023.07.03 |
함수 및 methods에 type alias 사용법 (0) | 2023.07.03 |
Literal Types (0) | 2023.07.03 |
타입 정의가 길고 복잡하다면 type alias & readonly (0) | 2023.07.02 |

interface 문법
interface Square {
color :string,
width :number,
}
let 네모 :Square = { color : 'red', width : 100 }
type 문법이랑 크게 다를 것은 없다.
차이점을 설명하자면
1. 중복선언이 가능하다
interface Student {name : string}
interface Student {score : number}
Student라는 인터페이스 안에는 name과 score 변수가 둘 다 존재한다.
extends 키워드 사용 전
interface Student {
name :string,
}
interface Teacher {
name :string,
age :number
}
다른 인터페이스이지만 중복되는 변수가 존재한다면 어딘가 불편함이 생긴다.
extends 키워드 사용 후
interface Student {
name :string,
}
interface Teacher extends Student {
age :number
}
extends 키워드 사용.
=> 부모에게 상속을 받아 중복되는 변수를 따로 선언할 필요가 없다.
편리함
그럼 type 키워드는 상속개념이 없나?
type Animal = {
name :string
}
type Cat = Animal & { age: number }
상속개념과는 다르지만 &연산자를 이용한다면 비슷한 원리로 작동할 수 있다.
1. interface도 &연산자를 사용할 수 있다.
2. &연산자는 intersection이라고 칭하며 교차해달라는 의미이다.
=> 즉 왼쪽타입과 오른쪽타입을 전부 만족하는 타입을 생성한다는 개념이라고 생각하면 이해하기 편하다.
조금 다르긴 하지만,
interface는 java공부할 때 많이 익숙한 친구라 쉽게 다가갈 수 있었다.
엄격함을 기준으로 봤을 때는 중복성 제한이 있는 type이 타입스크립트에서는 더 맞는 표현 같지만
interface는 추후에 타입을 더하거나 다른 사람의 코드를 수정할 일이 생긴다면 편리하게 애용할 것으로 예상된다.
'TypeScript' 카테고리의 다른 글
Narrowing 추가적인 방법들 (0) | 2023.07.03 |
---|---|
rest parameter , spread operator, destructuring 타입지정 (0) | 2023.07.03 |
함수 및 methods에 type alias 사용법 (0) | 2023.07.03 |
Literal Types (0) | 2023.07.03 |
타입 정의가 길고 복잡하다면 type alias & readonly (0) | 2023.07.02 |