-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
SWRMutation.trigger infers to undefined #2280
Comments
I'm with you here. I don't know under what conditions |
The type below is the current definition of /**
* Function to trigger the mutation. You can also pass an extra argument to
* the fetcher, and override the options for the mutation hook.
*/
trigger: [ExtraArg] extends [never]
? <SWRData = Data>(
extraArgument?: null,
options?: SWRMutationConfiguration<
Data,
Error,
ExtraArg,
SWRMutationKey,
SWRData
>
) => Promise<Data | undefined>
: <SWRData = Data>(
extraArgument: ExtraArg,
options?: SWRMutationConfiguration<
Data,
Error,
ExtraArg,
SWRMutationKey,
SWRData
>
) => Promise<Data | undefined> I currently have this workaround written in one of my codebases: Known IssuesSince SWR 2.0.0The return type of the // This is a simple one-off example.
// Please, use the existing rationale, code and folder structure for API calls.
const { trigger: createTodo } = useSWRMutation("/api/todos", async (key, { arg }) => {
const response = await axios.post<CreateTodoResponseData>(key, arg)
return response.data
})
const handleSubmit = useCallback(async (data) => {
// Note the exclamation mark.
// From: CreateTodoResponseData | undefined
// To: CreateTodoResponseData
const createdTodo = (await createTodo(data))!
}, [createTodo]) |
I would love either an explanation why it can be undefined, or a fix to the types as well. The docs don't mention anything: https://swr.vercel.app/docs/mutation#useswrmutation-return-values |
Bug report
Observed Behavior
The new
useSWRMutation->trigger
method infers toFetcherResponse<Data> | undefined
Expected Behavior
The new
useSWRMutation->trigger
method should infer toFetcherResponse<Data>
Repro Steps / Code Example
Additional Context
Versions:
Affected Code
swr/mutation/types.ts
Lines 58 to 61 in 2eb83c9
Integrated Example
When trying to use queries and mutations the types will clash because
mutate
expects an result notundefined
.See the inline comment below:
The text was updated successfully, but these errors were encountered: