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

type inference when using a curried generic on parseResponse #10

Open
L-Blondy opened this issue May 7, 2024 · 1 comment
Open

type inference when using a curried generic on parseResponse #10

L-Blondy opened this issue May 7, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@L-Blondy
Copy link
Owner

L-Blondy commented May 7, 2024

Using a non generic curried parser there is no issue

const upfetch = up(fetch)
const curried = () => (res: Response) => res.text()

const data = await upfetch('', {
   parseResponse: curried(),
   transform: (data) => data,
   onSuccess(data) {
      // ✅ ok
      expectTypeOf(data).toEqualTypeOf<string>()
   },
}) 
// ✅ ok
expectTypeOf(data).toEqualTypeOf<string>()

When using a curried parser everything is ok...

const data = await upfetch('', {
   parseResponse: withZod(z.object({ a: z.string() })),
})
// ✅ ok
expectTypeOf(data).toEqualTypeOf<{ a: string }>()

...until either onSuccess or transform are defined

const data = await upfetch('', {
   parseResponse: withZod(z.object({ a: z.string() })),
   transform: (data) => {
      // ❌ This fails, data is `any`
      expectTypeOf(data).toEqualTypeOf<{ a: string }>()
   }
})
// ❌ This fails, data is `any`
expectTypeOf(data).toEqualTypeOf<{ a: string }>()

but it works fine when using the functional version of the fetcher options

const data = await upfetch('', () => ({
   parseResponse: withZod(z.object({ a: z.string() })),
   transform: (data) => {
      // ✅ OK
      expectTypeOf(data).toEqualTypeOf<{ a: string }>()
   }
}))
// ✅ OK
expectTypeOf(data).toEqualTypeOf<{ a: string }>()

Also fine when unwrapping the curried function

const data = await upfetch('', {
   parseResponse: (res, opts) => withZod(z.object({ a: z.string() }))(res, opts),
   transform: (data) => {
      // ✅ OK
      expectTypeOf(data).toEqualTypeOf<{ a: string }>()
   }
})
// ✅ OK
expectTypeOf(data).toEqualTypeOf<{ a: string }>()

When using a default parser the data type in the callbacks come from the default parser, not the fetcher's

const upfetch = up(fetch, () => ({
   parseResponse: () => 1
}))

const data = await upfetch('', {
    Error on `parseResponse`
   parseResponse: withZod(z.object({ a: z.string() })),
   transform: (data) => {
      // ❌ This fails, data is `number`
      expectTypeOf(data).toEqualTypeOf<{ a: string }>()
   },
   onSuccess: (data) => {
      // ❌ This fails, data is `number`
      expectTypeOf(data).toEqualTypeOf<{ a: string }>()
   }
})
// ❌ This fails, data is `any`
expectTypeOf(data).toEqualTypeOf<{ a: string }>()

Same thing with the functional options, it works fine

const upfetch = up(fetch, () => ({
   parseResponse: () => 1
}))

const data = await upfetch('', {
   parseResponse: withZod(z.object({ a: z.string() })),
   transform: (data) => {
      // ✅ OK
      expectTypeOf(data).toEqualTypeOf<{ a: string }>()
   },
   onSuccess: (data) => {
      // ✅ OK
      expectTypeOf(data).toEqualTypeOf<{ a: string }>()
   }
})
// ✅ OK
expectTypeOf(data).toEqualTypeOf<{ a: string }>()

That's quite strange...

@L-Blondy L-Blondy self-assigned this May 7, 2024
@L-Blondy L-Blondy added the bug Something isn't working label May 7, 2024
@L-Blondy
Copy link
Owner Author

workaround

Until a solution is found, when using a validation adapter use the functional version of the fetcher options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant