Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[01/31] 5장 #80

Open
bada308 opened this issue Jan 30, 2024 · 0 comments
Open

[01/31] 5장 #80

bada308 opened this issue Jan 30, 2024 · 0 comments
Assignees

Comments

@bada308
Copy link
Collaborator

bada308 commented Jan 30, 2024

예시

extends 조건부 타입을 활용하여 개선하기

154 페이지의 예시 코드

type PayMethodType<T extends "card | "appcard" | "bank"> = T extends
    | "card"
    | "appcard"
    | Card
    : Bank;

export const useGetRegisteredList = <T extends "card" |  "appcard" |  "bank">(
    type: T
): UseQueryResult<PayMethodType<T>[]> => {
    // ...

   const result = useCommonQuery<PayMethodType<T>[]>(url, undefined, fetcher);

    return result;
}

위 예시에서 기대한 return 값은 PocketInfo<Card>[] | undefinedPocketInfo<Bank>[] | undefined 라고 이해했습니다.

기대한대로 동작하려면 PayMethodType의 타입 정의가 아래와 같이 바뀌어야 한다고 생각하는데 어떻게 생각하시나요??

type PayMethodType<T extends "card | "appcard" | "bank"> = T extends
    | "card"
    | "appcard"
    | PocketInfo<Card>
    : PocketInfo<Bank>;

infer을 활용해서 타입 추론하기

예시 코드 중 MenuNames에 관한 코드가 복잡해서 이해가 어려웠습니다..

type UnpackMenuNames<T extends ReadonlyArray<MenuItem>> = T extends
ReadonlyArray<infer U>
    ? U extends MainMenu
        ? U["subMenu"] extends infer V
            ? V extends ReadonlyArray<SubMenu>
                ? UnpackMenuNames<V>
                : U["name"]
            : never
        : U extends SubMenu
        ? U["name"]
        : never
    :never;
image

제가 이해한 내용인데 이게 맞는지 한 번 이야기 해보고 싶습니다~!

@bada308 bada308 self-assigned this Jan 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant