Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- ps
- review
- ML
- html
- frontend
- Express
- DM
- CSS
- vscode
- sqlite
- ts
- figma
- postgresql
- API
- DB
- js
- SOLID
- C++
- UI
- threejs
- CV
- GAN
- react
- Linux
- mongo
- python
- Git
- nodejs
- PyTorch
- PRISMA
Archives
- Today
- Total
아카이브
[TS] Syntax | 05. 타입 별칭(Type Alias) 본문
아래 코드는 cart 변수에 string[ ] 타입을 부여했습니다.
const cart: string[] = [
'c001',
'c001',
'c002',
];
interface User {
username: string;
email: string;
cart: string[];
}
const user: User = {
username: 'rayi',
email: 'jusmint3@gmail.com',
cart,
}
이 때 cart 변수의 타입을 바꾸려면 cart가 선언된 모든 구문을 찾아 string[ ]을 수정해주어야 합니다.
만약 더 복잡한 형태의 타입으로 정의되어 있다면, 이런 방식으로는 유지보수가 어려울 수 있습니다.
복잡한 타입에 이름을 붙여 간단하게 표기하기위해, type 구문을 사용할 수 있습니다.
// 변수
type Cart = string[];
// 함수
type CartResultCallbak = (result:boolean) => void;
// 객체
type Product = {
id: string;
name: string;
}
// Union
type Coupon =
| PromotionCoupon
| EmployeeCoupon
| WelcomCoupon
| RewardCoupon
;
위 예시에서 객체의 경우 interface와 유사하다는 것을 알 수 있는데, 실제로 interface와 동일한 동작을 수행합니다.
그러나 되도록이면 interface를 사용하는 것을 권장하는 편입니다.
728x90
'Language > TypeScript' 카테고리의 다른 글
[TS] Syntax | 07. keyof & typeof (0) | 2024.07.10 |
---|---|
[TS] Syntax | 06. Union & Intersection (0) | 2024.06.28 |
[TS] Type | 02. Literal 타입 (1) | 2024.06.28 |
[TS] Syntax | 04. Interface (0) | 2024.06.28 |
[TS] type | 01. 열거형(enum) (0) | 2024.06.28 |
Comments