【TypeScript】type파? interface파? 난 물론 type파
7841 단어 ReactTypeScript
TypeScript?
형식화할 수 있는 JavaScript
let age: number = 0;
age = 28;
age = "ぞんぞん"; //エラー
다양한 유형
금형
의미
예
문자열
문자열
'젠젠'
number
수치
28
부울
진위값
true
[]
배열
string[] → ['aaa', 'bbb', 'ccc']
any
제한 없음
객체의 유형은?
보통 쓰면 읽기 어렵고 관리하기 어렵습니다 🥺 (파온)
const miyazon: {name: string, age: number, department: string} = {
name: "みやぞん",
age: 28,
department: "product"
}
interface vs type(형 별칭)
객체의 형태 지정에는 interface 또는 type (형 별칭)을 사용한다
인터페이스
interface Employee {
name: string;
age: number;
department: string;
}
const miyazon: Employee = {
name: "みやぞん",
age: 28,
department: "product"
}
객체의 형태 지정에는 interface 또는 type (형 별칭)을 사용한다
type(형 별칭)
type Employee = {
name: string;
age: number;
department: string;
}
const miyazon: Employee = {
name: "みやぞん",
age: 28,
department: "product"
}
interface와 type의 차이
인터페이스
유형
용도
클래스, 객체의 타입 정의
유형에 별명을 붙인다
교차형, 공용체형, 튜플형, 맵형
비대응
대응
같은 요소명의 재선언
확장하다
할 수 없다
"용도의 차이"를 느낀다 (interface)
VS Code등으로 이름을 호버했을 때, 형 본래의 정보를 내 줍니다만, 여기서 차이가 나옵니다.
interface는 형태 그 자체이므로 이름이 나옵니다.

"용도의 차이"를 느낀다 (type)
익명의 타입에 별명을 붙였을 뿐이므로, 본래의 「익명의 타입」이 표시됩니다.

'같은 요소 이름 재선언'을 느낀다
type에서는 재선언했을 때에 에러가 되지만 interface에서는 어떻게 되는...?
interface Employee {
name: string;
}
interface Employee {
age: number;
}
interface Employee {
department: string;
}
'같은 요소 이름 재선언'을 느낀다
오류가되지 않습니다!
interface Employee {
name: string;
}
interface Employee {
age: number;
}
interface Employee {
department: string;
}
const miyzon: Employee = {
name: "ぞんぞん",
age: 28,
department: "product"
}
어느 쪽을 사용합니까?
let age: number = 0;
age = 28;
age = "ぞんぞん"; //エラー
보통 쓰면 읽기 어렵고 관리하기 어렵습니다 🥺 (파온)
const miyazon: {name: string, age: number, department: string} = {
name: "みやぞん",
age: 28,
department: "product"
}
interface vs type(형 별칭)
객체의 형태 지정에는 interface 또는 type (형 별칭)을 사용한다
인터페이스
interface Employee {
name: string;
age: number;
department: string;
}
const miyazon: Employee = {
name: "みやぞん",
age: 28,
department: "product"
}
객체의 형태 지정에는 interface 또는 type (형 별칭)을 사용한다
type(형 별칭)
type Employee = {
name: string;
age: number;
department: string;
}
const miyazon: Employee = {
name: "みやぞん",
age: 28,
department: "product"
}
interface와 type의 차이
인터페이스
유형
용도
클래스, 객체의 타입 정의
유형에 별명을 붙인다
교차형, 공용체형, 튜플형, 맵형
비대응
대응
같은 요소명의 재선언
확장하다
할 수 없다
"용도의 차이"를 느낀다 (interface)
VS Code등으로 이름을 호버했을 때, 형 본래의 정보를 내 줍니다만, 여기서 차이가 나옵니다.
interface는 형태 그 자체이므로 이름이 나옵니다.

"용도의 차이"를 느낀다 (type)
익명의 타입에 별명을 붙였을 뿐이므로, 본래의 「익명의 타입」이 표시됩니다.

'같은 요소 이름 재선언'을 느낀다
type에서는 재선언했을 때에 에러가 되지만 interface에서는 어떻게 되는...?
interface Employee {
name: string;
}
interface Employee {
age: number;
}
interface Employee {
department: string;
}
'같은 요소 이름 재선언'을 느낀다
오류가되지 않습니다!
interface Employee {
name: string;
}
interface Employee {
age: number;
}
interface Employee {
department: string;
}
const miyzon: Employee = {
name: "ぞんぞん",
age: 28,
department: "product"
}
어느 쪽을 사용합니까?
interface Employee {
name: string;
age: number;
department: string;
}
const miyazon: Employee = {
name: "みやぞん",
age: 28,
department: "product"
}
type Employee = {
name: string;
age: number;
department: string;
}
const miyazon: Employee = {
name: "みやぞん",
age: 28,
department: "product"
}
interface Employee {
name: string;
}
interface Employee {
age: number;
}
interface Employee {
department: string;
}
interface Employee {
name: string;
}
interface Employee {
age: number;
}
interface Employee {
department: string;
}
const miyzon: Employee = {
name: "ぞんぞん",
age: 28,
department: "product"
}
형에 빠지지 않는 인간이 되고 싶지만, type을 선택합니다.
「복업 클라우드」에 대해서
당사 Another works에서는 복업 매칭 플랫폼 「복업 클라우드」를 개발하고 있습니다!
▼복업으로 스킬을 살려 보지 않겠습니까? 복업 클라우드의 등록은 이쪽!
htps : // 타탄 t. 아 w- 그건 r rks. 이 m/?ぉ 긴_ty ぺ =
Reference
이 문제에 관하여(【TypeScript】type파? interface파? 난 물론 type파), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/hk206/items/5597872ce62bceb93c6a
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
Reference
이 문제에 관하여(【TypeScript】type파? interface파? 난 물론 type파), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/hk206/items/5597872ce62bceb93c6a텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)