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

Exercise 14: incorrect return type passes #2

Open
c-ehrlich opened this issue Sep 6, 2022 · 0 comments
Open

Exercise 14: incorrect return type passes #2

c-ehrlich opened this issue Sep 6, 2022 · 0 comments

Comments

@c-ehrlich
Copy link

Currently both of these "solutions" pass exercise 14:

// correct
const genericFetch = <Schema extends z.ZodSchema>(
  url: string,
  schema: Schema
): Promise<z.infer<Schema>> => {
  return fetch(url)
    .then((res) => res.json())
    .then((result) => schema.parse(result));
};
// incorrect, doesn't return a promise
const genericFetch = <Schema extends z.ZodSchema>(
  url: string,
  schema: Schema
): z.infer<Schema> => {
  return fetch(url)
    .then((res) => res.json())
    .then((result) => schema.parse(result));
};

This is because the current test case checks the type of result, which awaits genericFetch, so it doesn't care if genericFetch returns a Promise or not.

I came up with two possibilities for a test case that catches this - I don't love either of these, maybe a TS Wizard could come up with something more elegant.

  1. Just check that genericFetch returns a promise of some sort
Expect<Equal<ReturnType<typeof genericFetch>, Promise<z.TypeOf<any>>>>
  1. Check the entire type of genericFetch
Expect<
  Equal<
    typeof genericFetch,
    <Schema extends z.ZodType<any, z.ZodTypeDef, any>>(
      url: string,
      schema: Schema
    ) => Promise<z.TypeOf<Schema>>
  >
>
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