-
-
Notifications
You must be signed in to change notification settings - Fork 51
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
@redux-requests/react: ordinary event passed into dispatchRequest produces typescript error #433
Comments
@avasuro hmm, indeed, it is a wrapper from js perspective, but it changes its type from ts perspective. I don't think it should support custom Redux action though, because it also makes |
you could also create a wrapper of yours for this hook, which could work like |
Anyway, with the newest version, it is very rare to dispatch request actions manually, see |
@klis87 I'm not 100% sure but from my point of view it is worth to make it accept ordinary actions, because:
|
Thanks for your valid points, probably we could use TS conditional types to make it work for both cases, do you feel like creating a PR? ;) |
I proposed a PR with possible fix, please take a look, and make your decision about expected usage of dispatchRequest method basing on our discussion and this PR. Thank you for you efforts, I use new version of this library first time and i like it, API become better :) |
Thx, I will take a look! Also, if you have time, I would appreciate your opinions in issues labeled as |
@avasuro The next version won't have export interface Dispatch {
<Action = AnyAction>(action: Action): Action extends RequestAction<
any,
infer Data
>
? Promise<{
data?: Data;
error?: any;
isAborted?: true;
action: any;
}>
: Action;
}
interface RequestsStore extends Store {
dispatch: Dispatch;
} So as you can see, store will just have |
@klis87 Thank you, very good solution! |
Hello @klis87 . I'm having problems understanding the import { useDispatchRequest } from "@redux-requests/react";
...
const CreateCase: React.FC<CreateCaseProps> = (props) => {
const dispatch = useDispatchRequest();
const handleSubmit = async (data: any) => {
const { data, error } = await dispatch(createCase(data));
if (error) {
...
}
}
return <Button onClick={() => handleSubmit(caseForm)}>Create</Button>;
} I get a I cannot see what I'm doing wrong here, any suggestions would be appreciated. |
@john-wennstrom The only reason dispatch could reject promise is a syntax error, probably in your request action. Please show me |
@klis87 Yes, I now see why it didn't work. Forgot to return something from the request action. |
@john-wennstrom ok, good you found the problem, co closing then |
Actual behaviour
TypeScript throws type error when non-RequestAction instance is passed into dispatchRequest function.
Expected behaviour
TypeScript doesn't throw types error when non-RequestAction is passed into dispatchRequest function
Bug description:
According to documentation dispatchRequest function is just a wrapper for native redux dispatch function, and used only to add proper type checks. From my point of view that should mean that his function should be able to accept RequestAction as well as any other ordinary redux action. And it does accepts, but because of wrong typescript typings for that function typescript fails with error if I try to pass any non-RequestAction instance to this function.
Code example
Live example
_(this code will not work because react hook is used outside any react component, redux is not initialised etc. - I tried to make code as short as possible to focus only on main issue - typescript error)
In case of live example not works - there is the source code:
The text was updated successfully, but these errors were encountered: