타입스크립트 ...rest, destructuring

column clash·2021년 8월 22일
0
function 함수(...a: number[]) {
  console.log(a);
}

함수(1, 5, 3, 5, 6, 6);

let arr = [1, 2, 3];
let arr2 = [4, 5];
let arr3 = [...arr, ...arr2];
console.log(arr3);

let person = { student: true, age: 20 };

function 디스트럭쳐함수({ student, age }: { student: boolean; age: number }) {
  console.log(student, age);
}
디스트럭쳐함수({ student: true, age: 20 });

function 최댓값(...x: number[]) {
  let result = 0;
  x.forEach((i) => {
    if (result < i) {
      result = i;
    }
  });
  return result;
}

type UserType = {
  user: string;
  comment: number[];
  admin: boolean;
};

function 구조분해함수({ user, comment, admin }: UserType): void {
  console.log(user, comment, admin);
}

구조분해함수({ user: "kim", comment: [3, 5, 4], admin: false });

type 어레이 = (number | string | boolean)[];

function 구조분해배열함수([a, b, c]: 어레이) {
  console.log(a, b, c);
}

구조분해배열함수([40, "wine", false]);
profile
풀스택 개발 중...

0개의 댓글