-
유틸리티 타입 (TS)카테고리 없음 2023. 12. 20. 12:26
Partial 일부 속성만 제공하는 객체 interface Person { name: string; age: number; } const updatePerson = (person: Person, fields: Partial): Person => { return { ...person, ...fields }; }; const person: Person = { name: "Spartan", age: 30 }; const changedPerson = updatePerson(person, { age: 31 }); 이와 같이 속성을 이름, 에이쥐의 속성을 제시하면 이름만, 에이쥐만, 이름과 에이쥐가 같이만 들어 올 수 있도록 하는 속성이다. Required 모든 속성이 전부 제공되는 객체를 생성해야한다. inter..
-
TIL 2023 12 19카테고리 없음 2023. 12. 19. 21:04
https://mokieebetts.tistory.com/66 https://mokieebetts.tistory.com/65 https://mokieebetts.tistory.com/67 https://mokieebetts.tistory.com/68 https://mokieebetts.tistory.com/69 https://mokieebetts.tistory.com/70 https://mokieebetts.tistory.com/71 https://mokieebetts.tistory.com/72 https://mokieebetts.tistory.com/73 https://mokieebetts.tistory.com/74 https://mokieebetts.tistory.com/75 https://mokie..
-
. enum과 object literal카테고리 없음 2023. 12. 19. 19:53
ENUM 복습 이넘은 열거형 데이터 타입 상수의 그룹화를 위해선 이넘이 아주 좋은 타입 코드의 가독성이 높아지고 명확한 상수값을 정의할 수 있다. 또한, 컴파일시 자동으로 숫자로 매핑되므로 따로 값을 할 당할 필요없다. enum UserRole { ADMIN = "ADMIN", EDITOR = "EDITOR", USER = "USER", } enum UserLevel { NOT_OPERATOR, // 0 OPERATOR // 1 } function checkPermission(userRole: UserRole, userLevel: UserLevel): void { if (userLevel === UserLevel.NOT_OPERATOR) { console.log('당신은 일반 사용자 레벨이에요'); } ..
-
any와 unknown, union(TS)카테고리 없음 2023. 12. 19. 19:16
어쩔수 없는 가변적인 타입의 데이터를 저장하고 싶다면 ANY보다는 UNKNNWON을 쓰는게 좋다. ANY - UNKNOWN은 어떠한 타입이 올지 모르겠다면 그때 쓴다. ANY 타입 ANY타입은 모든 타입에 사용가능한 슈퍼 타입 어떠한 타입이든 저장이 가능하다 let anything: any; anything = 5; // 최초에는 숫자를 넣었지만 anything = 'Hello'; // 문자열도 들어가고요 anything = { id: 1, name: 'John' }; // JSON도 들어가네요 정말 아무거나 들어갈 수 있는 그런 타입으로 쓰인다. 즉, 넘버도 스트링도모두 들어간다 이게 아니었다면 에러가 났을 것이다. 정말 어쩔 수 없이 써야할 때 예를들면 RDB같은 포맷은 스키마에 맞게 들어오는데 NO..
-
CONST, READONLY, LET카테고리 없음 2023. 12. 19. 17:46
LET LET이란 키워들르 사용하면 변수가 된다. 번수는 값을 변경할 수 있다. let num: number = 5; console.log(num); // 출력: 5 num = 10; console.log(num); // 출력: 10 CONST , READONLY 는 불변한 상수선언이다. CONST 불변의 상수 선언으로 다시 선언이 불가능하다. const num: number = 5; console.log(num); // 출력: 5 num = 10; // 에러: 'num'은 const로 선언되었으므로 다시 할당될 수 없어요! const nums: number[] = []; console.log(nums); // 출력: [] nums.push(1); // 할당은 되지 않아도 배열에 데이터를 추가/삭제하는 ..