![[TypeScript] TypeScript 공부일지: 대수 타입 [6/7 study]](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdna%2FEQtpe%2FbtsHZcLdIm6%2FAAAAAAAAAAAAAAAAAAAAAFI0m6SnrR0vOrLUlclILwmWHIzVBUJ0UMEmqz4bE3_n%2Fimg.png%3Fcredential%3DyqXZFxpELC7KVnFOS48ylbz2pIh7yKj8%26expires%3D1753973999%26allow_ip%3D%26allow_referer%3D%26signature%3DVp3grhgWAYyKIbbnGgw%252BB%252FqJr%252FQ%253D)
[TypeScript] TypeScript 공부일지: 대수 타입 [6/7 study]Frontend/TypeScript2024. 6. 7. 16:05
Table of Contents
TypeScript 공부일지: 대수 타입
❗️주의
지극히 개인 공부한 내용을 올린 거기 때문에 이해하지 못할 수도 있음
❗️대수 타입이란?
- 여러개의 타입을 합성해서 새롭게 만들어낸 타입
- 합집합, 교집합 타입이 존재함
/**
* 1. 합집합 타입(Union 타입)
*/
let a: string | number | boolean;
a = "hello";
a = 1;
a = true;
let arr: (number | string | boolean)[] = [1, "hello", true];
type Dog = {
name: string;
color: string;
};
type Person = {
name: string;
language: string;
};
type Union1 = Dog | Person;
let union1: Union1 = {
name: "",
color: "",
};
let union2: Union1 = {
name: "",
language: "",
};
let union3: Union1 = {
name: "",
color: "",
language: "",
};
// 두 타입이 모두 공통으로 갖는 프로퍼티만 넣으면 오류나옴
// let union4: Union1 = {
// name: ""
// }
/**
* 2. 교집합 타입 - Intersection Type
*/
let variable: number & string;
type Cat = {
name: string;
color: string;
};
type Human = {
name: string;
language: string;
};
type Intersection = Cat & Human;
let intersection: Intersection = {
name: "",
color: "",
language: "",
};
@동혁이 :: Eun_Frontend
프론트엔드 공부일지 입니다.
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!