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 | 31 |
Tags
- 파이썬
- stdin vs input
- k for k
- kotlin
- Android
- JS
- 파이어베이스
- 안드로이드
- next Link
- react
- HTML
- 백준 스택
- C++
- nodejs
- 프론트엔드
- TS
- firebase
- 자바스크립트
- 리액트
- 백준 스택 시간초과 python
- NPM
- 타입스크립트
- 최적화
- 알고리즘
- CSS
- Python
- 코딩테스트
- typescript
- 스택
- javascript
Archives
- Today
- Total
sooleeandtomas
TS - interface 본문
TS에는 interface라고, 파라미터 값이 어떤 '타입'을 갖는지 정의해주는 기능이 있다.
예시1) 로그인 ajax요청할 때, 아이디, 패스워드, 이메일 값을 string으로 받아와야 한다.
interface singupType{
id:string;
password:string;
email:string;
}
function ajaxSignup(data:singupType){
}
ajaxSingup({
//vsc에서 자동완성으로 어떤 타입을 넣어야 하는지 알 수 있음
})
예시2) tv는 꼭 turnOn , turnOff 가 되어야 한다.
interface TV{
turnOn():boolean;
turnOff():void;
}
const myTv:TV={
turnOn(){
return true
}
turnOff(){}
}
function tryTurnOn(tv:TV){
tv.turnOn()
}
tryTurnOn(myTv)
✨ ts->js
const myTv={
turnOn(){
return true
}
turnOff(){}
}
function tryTurnOn(tv){
tv.turnOn()
}
tryTurnOn(myTv)
예시3) row:숫자 col:숫자 piece:row,col에서 움직여야 한다.
interface Cell{
row:number;
col:number;
piece?:PieceType
}
interface PieceType{
move(from:Cell to:Cell):boolean
}
function createBoard(){
const cells:Cell[]=[];
for(row=0;row<4;row++){
for(cell=0;cell<3;cell++){
cells.push({row,col})
}
}
}
const board= createBoard();
board[0].piece={
move(from:Cell to:Cell){
return true
}
}
✨ ts->js
function createBoard(){
const cells=[];
for(row=0;row<4;row++){
for(cell=0;cell<3;cell++){
cells.push({row,col})
}
}
}
const board= createBoard();
board[0].piece={
move(from,to){
return true
}
}
'코딩 공부 노트 > TS' 카테고리의 다른 글
TS - enum (0) | 2020.07.31 |
---|---|
TS - annotation (0) | 2020.07.31 |
TS - 사용법 익히기 (0) | 2020.07.31 |
npm install brew install (0) | 2020.07.30 |
Comments