Better Fetch

Deafult Types

Default Output

By default the response data will always be type of unknown. If you want to customize the deafult type you can pass defaultOutput option to the createFetch function.

This only serve as a type for the response data it's not used as a validation schema.

fetch.ts
import {  } from "@better-fetch/fetch";
import {  } from "zod";
 
const  = ({
    : "https://jsonplaceholder.typicode.com",
    : .(),
})
 
const { ,  } = await ("/todos/1")
 
 
Hover over the data object to see the type

If you define output schema, the default output type will be ignored.

fetch.ts
import {  } from "@better-fetch/fetch";
import {  } from "zod";
 
const  = ({
    : "https://jsonplaceholder.typicode.com",
    : .(),
});
 
const { ,  } = await ("/todos/1", {
    : .({
        : .(),
        : .(),
        : .(),
        : .(),
    }),
})
 
Hover over the data object to see the type

Default error

The default error type is:

{ status: number, statusText: string, message?: string }. 

if you want custom defualt error type, you can pass a defautlError option to the createFetch function.

The status and statusText properties are always defined. Your custom error definations are only infered if the api returns a json error object.

fetch.ts
import {  } from "@better-fetch/fetch";
import {  } from "zod";
 
const  = ({
    : "https://jsonplaceholder.typicode.com",
    : .({
        : .().(),
        : .(),
    }),
})
 
const { ,  } = await ("/todos/1")
 
Hover over the error object to see the type

On this page