일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 무한 스크롤
- 인터섹션
- 투포인터
- TS
- 결정 알고리즘
- autosize
- useAppDispatch
- async/await
- 리터럴 타입
- RTK Query
- 이분 검색
- tailwind
- webpack
- recoil
- 타입 좁히기
- app router
- ESlint
- CORS
- Promise
- 태그된 유니온
- Jest
- map
- SSR
- 반공변성
- CI/CD
- 호이스팅
- React
- 공변성
- dfs
- Cypress
- Today
- Total
목록TS (34)
짧은코딩
타입 만들기 forEach -예시1 const a: Arr = [1, 2, 3]; a.forEach((item) => { console.log(item); }); a.forEach((item) => { console.log(item); return "3"; }); 이 코드가 있을 때 Arr 타입을 추론할 수 있어야 한다. interface Arr { forEach(callback: (item: number) => void): void; } const a: Arr = [1, 2, 3]; a.forEach((item) => { console.log(item); }); a.forEach((item) => { console.log(item); return "3"; }); forEach 안에 콜백 함수가 있고 리..
제네릭 -틀린 예시 function add(x: string | number, y: string | number): string | number { return x + y; } add(1, 2); // 3 add("1", "2"); // '12' add(1, '2') // '12' add('1', 2) // '12' 타입을 이렇게 정하면 아래 2줄 같은 경우 때문에 에러가 난다. -제네릭 틀린 예시 function add(x: T, y: T): T { return x + y; } add(1, 2); // 3 add("1", "2"); // '12' add(true, false); add(1, "2"); // '12' add("1", 2); // '12' T는 다 같은 타입이라는 의미이다. 하지만 add(..
타입 가드 function numOrStr(a:number|string) { a.toFixed(1); } numOrStr('123'); numOrStr(1); 이렇게 하면 ts는 모든 가능성을 열어두기 때문에 오류가 난다. 왜냐하면 toFixed는 number에서만 사용할 수 있기 때문이다. toFixed는 매개변수 숫자만큼 소수점이 생긴다. function numOrStr(a: number | string) { if (typeof a === "number") { a.toFixed(1); } else { a.charAt(3); } } numOrStr("123"); numOrStr(1); 따라서 이렇게 표시하면 number인 것이 확실해서 오류가 발생하지 않는다. 그리고 else 문으로 가면 string인..
함수 리턴값 타입 위치 function add(x: number, y: number): number { return x + y; } 리턴값 타입 위치는 이렇게 매개변수 뒤쪽에 붙여야한다. function add(x: number, y: number): number; function add(x, y) { return x + y; } 혹은 이렇게 타입만 선언하고 다음에 내용을 선언하는 경우도 있다. 화살표 함수 const add: (x: number, y: number) => number = (x, y) => x + y; 이렇게 매개변수의 타입 위치가 찾기 어렵게 있다. 이런 경우 타입을 다 지웠을 때 js 문법에 맞게 되도록 코딩을 하면 어느정도 맞다. type Add = (x: number, y: num..