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
- 코드푸시
- array
- meatadata
- reactnative
- xlsx-js-style
- 글또10기x코드트리
- TS
- extends
- code-push-standalone
- map
- 이진탐색
- Spring
- react
- app.post
- 페이지네이션
- set
- 상속
- supabase auth
- codepush
- async
- 슬라이딩윈도우
- Next.js
- Filter
- interface
- 스크롤이벤트
- supabase authentication
- javascript
- 타입스크립트
- generic
- supabase 페이지네이션
Archives
- Today
- Total
rhanziy
TS - type alias와 readonly 본문
type 키워드를 써서 가독성과 확장성을 높일 수 있다. 간단쓰
type Animal = string | number | undefined;
let animal :Animal = 'cat';
// obj일 때
type Animal = { name :string, age :number }
let 동물 :Animal = { name : 'munzi', age : 10 }
const 변수는 등호할당만 막아지는데, 오브젝트 자료형일 경우 아래와 같이 실수로 수정을 할 수 있음.
const 출생지역 = { region : 'seoul' }
출생지역.region = 'busan'
타입스크립트에서는 readonly 기능을 통해 타입스크립트 자체에서 error를 띄워줄 수 있다.
type Cat = {
readonly name? :string
// = name : string | undefiend
}
const cat:Cat = {
name : '먼지'
}
cat.name = '둥지'; // error
type 끼리 extend도 가능.
type Name = string;
type Age = number;
type Person = Name | Age;
// &연산자로 obj 타입 extend
type PositionX = { x : number };
type PositionY = { y : number };
type NewType = PositionX & PositionY;
let position :NewType = { x : 10 , y : 20 };
좀 더 이해가 쉽게 연결시켜보면~
type User ={
name : string,
phone : number,
email : string
}
type Adult = {
adult : boolean
}
type NewUser = User & Adult;
let 회원가입정보 :NewUser = {
name : 'kim',
phone : 123,
email : '123@123',
adult : true
}
'Html_css_js' 카테고리의 다른 글
TS - 함수의 type alias (0) | 2023.01.05 |
---|---|
TS - Literal types로 값 지정하기 (1) | 2023.01.05 |
TS - type narrowing, assertion 으로 범위 지정해주기 (0) | 2023.01.03 |
TS - 타입스크립트 셋팅 및 필수문법(powershell 보안 오류) (0) | 2023.01.02 |
JS - new Map(), new Set() 사용하기 (0) | 2022.12.29 |
Comments