전체 글

전체 글

    A = [{ a:string, b:string}]와 type B = { a:string, b:string}[]의 차이가 뭘까요?

    하루가 질문했던 내용에 당일 해답을 주지 못해서 다음날 고민해보다가 해결한 기록입니다 글도 남길 겸, 타입스크립트 스터디와 재이에게 정보도 줄 겸! 이렇게 블로그를 작성했습니다 😉👍 멋진 팀원이죠? 얼른 본론으로 들어갈게요 문제가 뭐였나요? 하루가 질문한 내용은, 상위 컴포넌트에서 type A = [{ a:string, b:string }] type B = { a:string, b:string }[] A 타입으로 작성된 props를 받을 때, type을 B로 명시하면 발생하는 에러에 관한 것이었습니다. 배열의 크기는 항상 1로 일정하고, 똑같이 배열 안에 a, b string이 들어있는 객체를 보내는 것은 똑같은데, 이 타입이 무슨 차이가 있어서 달라질까요? 하는 내용이었습니다. 그래서 무슨 차이가 있나..

    [type-challenges] Merge

    문제 두개의 타입을 새로운 타입으로 병합하세요. 두번째 타입이 첫번째 타입을 덮어씁니다. 설명 /* _____________ Your Code Here _____________ */ type Merge = {[P in keyof F | keyof S]: P extends keyof S ? S[P] :P extends keyof F ? F[P]:never} /* _____________ Test Cases _____________ */ import type { Equal, Expect } from '@type-challenges/utils' type Foo = { a: number b: string } type Bar = { b: number c: boolean } type cases = [ Expect,..

    [type-challenges] String to Union

    문제 String을 Union type으로 변환하시오. output은 input으로 받은 string letters의 유니온 타입이어야 합니다 설명 /* _____________ Your Code Here _____________ */ type StringToUnion = T extends `${infer A}${infer B}` ? StringToUnion :P[number] /* _____________ Test Cases _____________ */ import type { Equal, Expect } from '@type-challenges/utils' type t = StringToUnion type cases = [ Expect, Expect, Expect, Expect, ] 우와~~ 제가 ..

    [type-challenges] Absolute

    문제 number, string 혹은 bigint를 받는 Absolute 타입을 만드시오 output은 양의 정수 string입니다 설명 /* _____________ Your Code Here _____________ */ type Absolute = `${T}` extends `${'-'}${infer P}`? P :`${T}` /* _____________ Test Cases _____________ */ import type { Equal, Expect } from '@type-challenges/utils' type t = Absolute type cases = [ Expect, Expect, Expect, Expect, Expect, Expect, Expect, Expect, Expect, E..

    [type-challenges] Append to object

    문제 주어진 인터페이스에 새로운 필드를 추가한 object 타입을 구현하세요. 이 타입은 세 개의 인자를 받습니다. 설명 /* _____________ Your Code Here _____________ */ type AppendToObject = {[P in keyof T | U]:P extends keyof T ? T[P] : V} /* _____________ Test Cases _____________ */ import type { Equal, Expect } from '@type-challenges/utils' type test1 = { key: 'cat' value: 'green' } type testExpect1 = { key: 'cat' value: 'green' home: boolean ..

    [type-challenges] Flatten

    문제 주어진 배열을 플랫한 배열 타입으로 바꾸는 Flatten 타입을 구현하세요. 설명 /* _____________ Your Code Here _____________ */ type Flatten = T extends [any:infer K, ...a: infer P] ? K extends any[] ? Flatten : Flatten :S /* _____________ Test Cases _____________ */ import type { Equal, Expect } from '@type-challenges/utils' type cases = [ Expect, Expect, Expect, Expect, Expect, ] // @ts-expect-error type error = Flatten 바로..

    [type-challenges] Length of String

    문제 String#length처럼 동작하는 문자열 리터럴의 길이를 구하세요. 설명 /* _____________ Your Code Here _____________ */ type LengthOfString = S extends `${string}${infer R}` ? LengthOfString:T['length'] /* _____________ Test Cases _____________ */ import type { Equal, Expect } from '@type-challenges/utils' type cases = [ Expect, Expect, Expect, Expect, ] 타입스크립트.. 배워도 배워도 어렵다... 아이디어: 배열 타입은 Arr['length']를 통해 전체 길이를 받을 수..

    [type-challenges] Append Argument

    문제 함수 타입 Fn과 어떤 타입 A가 주어질 때 Fn의 인수와 A를 마지막 인수로 받는 Fn과 동일한 함수 유형인 G를 생성하세요. 설명 type AppendArgument = Fn extends (...args: infer R) =>infer T ? (...args:[...R,A])=>T : never 우와... 함수 Fn은 args가 몇 개로 정해지지 않았기 때문에 어떻게 확장할까 고민했는데 (...args:infer R) => infer T ✨ 로 간단하게 사용할 수 있다는 것을 알았다. 그 다음은 다른 문제들과 같다. 또 Fn이 저 ✨을 extends하지 않을 시 never을 반환한다 ex type t = AppendArgument // never