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

how to get returnType of useSearch***Params? #3

Open
ddalpange opened this issue Jul 29, 2021 · 2 comments
Open

how to get returnType of useSearch***Params? #3

ddalpange opened this issue Jul 29, 2021 · 2 comments
Labels
enhancement New feature or request

Comments

@ddalpange
Copy link

I want to get returnType of useSomethingSearchParams('/sign-up')

I think I have to use ReturnType<ConvertTable[TPath]>;

but ConvertTable was not exported..

type Pathname = keyof ConvertTable;
export default function useSomethingSearchParams<TPath extends Pathname>(path: TPath) {
  const {query} = useRouter();
  if (!convertTable[path]) throw new Error(`unknown path: ${path}`);
  return convertTable[path](query) as ReturnType<ConvertTable[TPath]>;
}
type ConvertTable = typeof convertTable;
const convertTable = {
  '/sign-up': (query: ParsedUrlQuery) => {
    const result: any = {};
    {
      // "temporaryCode"
      const value = query['temporaryCode'];

      if (value == null) {
        result['temporaryCode'] = null;
      } else if (Array.isArray(value)) {
        result['temporaryCode'] = value[0];
      } else {
        result['temporaryCode'] = value;
      }
    }
    // ....
    return result as {
      readonly temporaryCode: string | null;
    };
  },
} as const;
@disjukr
Copy link
Contributor

disjukr commented Jul 29, 2021

ConvertTable이 직접 필요한 상황인가요?

useSomethingSearchParams('/sign-up')의 반환 타입이 알고싶은 경우는 실제로 호출도 해봐야 할 상황일 것 같은데,

const searchParams = useSomethingSearchParams('/sign-up');
type X = typeof searchParams;

이렇게 알아내는 방법으로는 부족할까요?

@ddalpange
Copy link
Author

@hyp3rflow hyp3rflow added the enhancement New feature or request label Feb 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants