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
Using mutate with optimisticData: typescript is giving me a hard time #2406
Comments
@shuding I checked out https://pkg.csb.dev/vercel/swr/commit/b4df51d9/swr and indeed now, the I do have a second issue, is there a reason why optimisticData can't be undefined? I have a pattern like this: const updateNotificationStatus = useCallback(
async (isEnabled: boolean) => {
await api.post('/direct-post/notification', {
coinId: asset.id,
isEnabled: isEnabled,
});
return undefined;
},
[asset.id],
);
const handleEnableNotifications = useCallback(
async (enabled: boolean) => {
try {
await mutate(updateNotificationStatus(enabled), {
optimisticData: (response) => {
if (!response) {
return undefined;
}
return {
...response,
data: {
...response.data,
notificationSetting: {
...response.data?.notificationSetting,
isEnabled: enabled,
},
},
};
},
populateCache: false,
revalidate: true,
});
} catch (e) {
ErrorHandler.catch(e);
}
},
[mutate, updateNotificationStatus],
);
(parameter) response: AxiosResponse<NotificationSettingApiResponse, any> | undefined
Type '(response: AxiosResponse<NotificationSettingApiResponse, any> | undefined) => { data: { notificationSetting: { isEnabled: boolean; }; }; ... 4 more ...; request?: any; } | undefined' is not assignable to type 'AxiosResponse<NotificationSettingApiResponse, any> | ((currentData?: AxiosResponse<NotificationSettingApiResponse, any> | undefined) => AxiosResponse<...>) | undefined'.
Type '(response: AxiosResponse<NotificationSettingApiResponse, any> | undefined) => { data: { notificationSetting: { isEnabled: boolean; }; }; ... 4 more ...; request?: any; } | undefined' is not assignable to type '(currentData?: AxiosResponse<NotificationSettingApiResponse, any> | undefined) => AxiosResponse<NotificationSettingApiResponse, any>'.
Type '{ data: { notificationSetting: { isEnabled: boolean; }; }; status: number; statusText: string; headers: AxiosResponseHeaders; config: AxiosRequestConfig<any>; request?: any; } | undefined' is not assignable to type 'AxiosResponse<NotificationSettingApiResponse, any>'.
Type 'undefined' is not assignable to type 'AxiosResponse<NotificationSettingApiResponse, any>'.ts(2322)
index.tsx(55, 27): Did you mean to call this expression? |
Edit: Posted this as an issue as well as it seems that the types in the library are wrong
Hi there,
Was just upgrading from 1.x to 2.x. I love the Optimistic UI approach, it's removing a lot of our boilerplate. I have one issue though, related to typescript.
I'm using swr like this:
Basically telling that we receive an
AxiosResponse
with a genericData
type inside it's data key. That's all good. Because of this,mutate
is automatically inferred to this type:And I mutate like this:
Code works perfectly, gets rolled back on error, all good. It's just that typescript is complaining that
updateNotificationStatus
is returning aPromise<void>
whilst it expectsPromise<AxiosResponse<NotificationSettingApiResponse, any>>
Anyone has found a way to tell typescript this is fine? Because if you set
populateCache: false
, it doesn't matter whatupdateNotificationStatus
returns, it's not used anyway.Hope this was clear 😅
Originally posted by @svenlombaert in #2358
The text was updated successfully, but these errors were encountered: