일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 프로그래머스 레벨 2
- 수학
- Nestjs
- 소켓
- javascript
- Algorithm
- dp
- 쉬운 문제
- 레벨 1
- Crawling
- 그래프
- dfs
- 타입 챌린지
- 타입스크립트
- socket
- 크롤링
- HTTP 완벽 가이드
- Node.js
- typescript
- TCP
- type challenge
- 프로그래머스
- 백준
- 알고리즘
- BFS
- 문자열
- ip
- HTTP
- 가천대
- 자바스크립트
- Today
- Total
목록프로그래밍/TypeScript (65)
kakasoo
it('predict', { only: true }, async () => { type SomeGenericType = T extends '' ? never : string; class TestClass { /** * Error: Error on typia.random(): non-specified generic argument. */ predict1() { return typia.random(); // complie error } /** * non-compile error, but if I call this `predict` method, it occur error. * * for example, `TypeError: $pick(...) is not a function` */ predict2() { r..
type Add1 = Length; type Add2 = [...NTuple, ...NTuple] extends [...infer U] ? Length : never; 흥미롭게도 Add1은 템플릿 문자열 타입 내에서 사용할 수 없다. 즉, 이 `${Add1}` 타입으로 변경하여, 수를 문자로 바꾸는 활용이 불가능한데, 이는 Length 타입의 적용 시점 차이로 보인다. 아직까지는 정확한 이유는 모르겠으나 infer 키워드를 이용한 방식이 문자열 템플릿 타입 내에서 숫자를 사용할 때 더 강력한 것으로 보인다. https://stackoverflow.com/questions/76243178/generic-type-for-adding-two-numbers Generic type for adding two nu..
type IsTuple = [T] extends [never] ? // T가 never 라면 무조건 false여야 한다. false : T extends readonly any[] ? // T가 readonly any[] 라면, number extends T['length'] ? // T의 length가 숫자 상수인지 아닌지 검증하기 위해 number extends T['length']를 한다. false : // 만약 true라면 T['length']는 number인 것이기에 튜플이 아니다. true : // 만약 false라면 T['length']는 숫자 상수 타입이기 때문에 튜플이 맞다. false; // 애초에 T가 readonly any[]가 아니라면 튜플이 아니다. 주석으로 설명을 대체한다.
type ToString= T extends string ? T : never; type ToStringTuple= T extends string[] ? T : never; type Join = T extends [infer F, ...infer Rest] ? `${ToString}${Join}` : ''; type Replace = C extends '' ? never : S extends `${infer F}${infer Rest}` ? F extends C ? [...Replace] : [F, ...Replace] : []; type DropChar = Join DropChar는 문자열에서 특정한 글자가 있을 경우 삭제하는 타입을 구현한다. 사실, 이는 이전에 구현한 R..
문자열로 이루어진 배열을 받아 Join하는 타입을 구현했다. type ToString= T extends string ? T : never; type ToStringTuple= T extends string[] ? T : never; type Join = T extends [infer F, ...infer Rest] ? Rest extends [] ? `${ToString}` : `${ToString}${U}${Join}` : ''; https://kscodebase.tistory.com/709 타입 레벨에서 문자열 Join 구현하기 2 https://kscodebase.tistory.com/682 타입 레벨에서 문자열 Join 구현하기 type ToString= T extends string ? T :..
type Reverse = T extends [infer F, ...infer Rest] ? [...Reverse, F] : []; 뒤집히진 배열의 타입을 추론한다.
type Shift = T extends [infer F, ...infer Rest] ? Rest : []; 타입 레벨에서 배열의 첫 요소를 제거하는 코드는 이렇게나 간단하다.
type IsPartitionSameEntire = T extends P ? P extends T ? true : false : false; type IsUnion = IsPartitionSameEntire extends true ? false : IsPartitionSameEntire extends false ? false : true; 조금 지저분한 풀이긴 하지만, 분산 법칙을 이용해서 푸는 방법이 있다. Equal 타입을 구현하다가 안 것인데, type Equal = X extends Y ? (Y extends X ? true : false) : false; type a5 = Equal; // boolean type a6 = Equal; // boolean Equal을 다음과 같이 X가 Y일 때 Y..
/** * wrong case */ // type StartsWith = `${U}${string}` extends `${T}` ? true : false; type StartsWith = T extends `${U}${string}` ? true : false; 처음에는 위에 주석처럼 풀고 동작하지 않는 것을 보았다. U에 string을 추가한 것만큼이 결국 T가 아닌가 생각했지만, 'ab'와 string을 조합한 것이 'abc'보다 크다는 걸 뒤늦게 깨달은 것이다. 그래서 extends 문에서의 비교 순서를 거꾸로 하여 T가 U에 string 타입을 더해 더 긴 문자열을 만든 것에 포함된다는 걸로 바꾸었다. type EndsWith = T extends `${string}..
Math Types(1) - 타입으로 사칙연산하기 타입스크립트 언어는 자바스크립트와 타입 레벨을 합친 언어다. 따라서 타입을 지정하는 것 뿐만 아니라 직접 타입을 구현할 수 있는 수준이 되어야 타입스크립트 쓰는 것이다. 하지만 타입 레 kscodebase.tistory.com 타입에서 나누기와 나머지 연산 구현하기 export type Divide = NTuple extends [...NTuple, ...infer Rest] ? Divide : Answer; export type Remainder = Sub; 일단 이 두 식들을 이용하면 타입에서도 나누기와 나머..
type Absolute = `${T}` extends `-${infer R}` ? R : `${T}`; 수는 문자열이 아닌데 어떻게 마이너스를 제외한 숫자 부분만 추출하지 고민했지만, 그렇다면 문자로 바꿔주면 될 뿐이었다. 처음의 T를 `${T}`로 바꿔준다는 생각만 닿으면 이후는 너무나 간단하다.
type Case0 = ['', '', ''] type Case1 = ['+', '', ''] type Case2 = ['+', '1', ''] type Case3 = ['+', '100', ''] type Case4 = ['+', '100', '%'] type Case5 = ['', '100', '%'] type Case6 = ['-', '100', '%'] type Case7 = ['-', ..