-
Notifications
You must be signed in to change notification settings - Fork 793
Typescript inconsistency: MutationFn
returns a promise that can resolved with void
?
#2095
Comments
@codepunkt I'm not entirely sure why it can be void, that was there before #1830. If I had to hazard a guess I think it's void if an error is thrown and an that being said, @jbaxleyiii will probably know for sure |
@brettjurgens @jbaxleyiii Yeah, it seems to be |
The current typing is non-optimal for the most common use case for everyone who uses TypeScript. For every mutation, an additional (useless) check has to be done against I even ended up creating a wrapper component which fixes the typing for me. I don't think |
Can you contribute that wrapper, as a comment here, an npm package or a gist? |
Hope it helps! import * as React from 'react'
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
import {
Mutation as __Mutation,
MutationProps as __MutationProps,
MutationOptions,
FetchResult,
} from "react-apollo";
// Apollo's typing can also return void (in case of onCompleted/onError callbacks):
// https://github.com/apollographql/react-apollo/issues/2095
// We never use that functionality so fix here
export declare type MutationFn<TResult, TVariables> = (options?: MutationOptions<TResult, TVariables>) => Promise<FetchResult<TResult>>;
type MutationProps<TResult, TVariables> = Omit<__MutationProps<TResult, TVariables>, "children"> & {
children: (mutateFn: MutationFn<TResult, TVariables>) => React.ReactNode;
}
export class Mutation<TResult, TVariables> extends React.Component<MutationProps<TResult, TVariables>> {
public render() {
const {
children,
...otherProps
} = this.props
return (
<__Mutation<TResult, TVariables> {...otherProps}>
{children as any}
</__Mutation>
)
}
} |
Two questions remain
|
Still facing the same void issue. Any newer news? Using 2.4.1 react apollo. |
Same here.
And it prints me error:
When I remove that 'void' in |
I'm experiencing this issue as well. I'm used to writing code like this: const result = await mutate(variables)
if (isDefined(result)) {
// work with the result
} but now I seem to need to do this: const result = await mutate(variables)
if (isAFetchResultAtAll(result)) {
if (isDefined(result)) {
// work with the result
}
} My feeling is that a function with a void return type is saying "do not use the return type of this function". I'm not sure what it means for a function to sometimes be void... If the function throws an exception, then the caller will either catch the exception or it will be re-thrown, in either case the return value is not available I think? I think this should probably be For now I will just be casting the return type: const result = _dangerouslyCastTo<FetchResult>(await mutate(variables)) |
Would be nice if authors could provide us much more examples for render props pattern + typescript. This page includes few, but still not enough I think. |
Going to bump this again since it's still an issue. Can people confirm void can safetly be ignored here? |
This was fixed in #3334, and will be published shortly. |
That's great it's been fixed for react-hooks in #3334, but I think this is still an issue when using react-components. I believe the type is defined here: react-apollo/packages/common/src/types/types.ts Lines 140 to 145 in 3c9ac0e
|
@Mike-Gibson would you mind opening a separate issue to track that change? Thanks! |
I'm confused: When can a
MutationFn
return a promise that is resolved withvoid
?This forces me to do things like this:
When i do a blame on
src/Mutation.tsx
, i can see this snippet being last edited in #1830When i have a look at the changes introduced by the pull request, it looks like this:
Promise<FetchResult<TData>>
! No clue where thevoid |
came from or why it's there./cc @brettjurgens @jbaxleyiii
The text was updated successfully, but these errors were encountered: