... 로 rest parameter를 만들어주면 배열로 받을 수 있다.function add(...a){
console.log(a)
}
add(1,2,3,4,5)
function add(...a:number[]){
console.log(a)
}
add(1,2,3,4,5)
기존 코드에서는 object안에 있는 데이터를 하나씩 빼서 적었어야 한다. 그런데 ES6문법에서는 한번에 값을 빼서 저장할 수 있다. object와 array 자료형 모두에서 쓸 수 있는데, object는 변수 이름을 속성이름과 동일하게 해줘야 하고, array는 아니다.
[기존코드]
const people = { student : true, age : 20 }
const student = people.student;
const age = people.age
[es6]
const { student, age } = { student : true, age : 20 }
const [a,b] = ["안녕", 100]
const person = { student : true, age : 20 }
function makeUser({student, age}){
console.log(student, age)
}
makeUser({ student : true, age : 20 })
const person = { student : true, age : 20 }
function makeUser({student, age}:{student:boolean, age:number}){
console.log(student, age)
}
makeUser({ student : true, age : 20 })
[연습장]
type userType = {
user :string,
comment :number[],
admin :boolean,
}
const makeUser = ({user, comment, admin} : userType):void => {
console.log(user, comment, admin);
}
makeUser( {user:"Kim", comment:[3,5,4], admin:false})
const wineMaker= ([year, type, isFree]:(number|string|boolean)[]):void => {
console.log(year, type, isFree);
}
wineMaker([40, 'wine', false])