-
Notifications
You must be signed in to change notification settings - Fork 141
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
Typescript types seem not to work properly #230
Comments
Okay I have fixed the types for us locally with this version: import { ComponentClass, FunctionComponent } from 'react';
import { connect as badConnect, Mapping, WithRefetch } from 'react-refetch';
type FixedConnect = <FetchProps, CompProps>(
map: MapPropsToRequestsToProps<FetchProps, CompProps>,
) => (
component: ComponentClass<CompProps> | FunctionComponent<CompProps>,
) => ComponentClass<Omit<CompProps, keyof FetchProps>> & WithRefetch<Omit<CompProps, keyof FetchProps>>;
type MapPropsToRequestsToProps<FProps, CProps> = (props: Omit<CProps, keyof FProps>, context: any) => PropsMap<FProps>;
type PropsMap<FProps> = {
[FProp in keyof FProps]: PromiseStateMapping<FProps> | FunctionMapping<FProps, FProp>;
};
type PropsMapInner<FProps> = {
[FProp in keyof FProps]?: PromiseStateMapping<FProps> | FunctionMapping<FProps, FProp>;
};
type PromiseStateMapping<FProps> = string | Mapping<FProps, any>;
type FunctionMapping<FProps, FProp extends keyof FProps> = FProps[FProp] extends ((...args: infer FArgs) => void)
? ((...args: FArgs) => PropsMapInner<FProps>)
: never;
export const connect: FixedConnect = badConnect as FixedConnect; It does not have defaults and options of connect, as we did not need it yet. |
Sorry for the VERY long delay on responding to this. @lmontoute would you happen to have any thoughts on this issue? Perhaps it would be helpful to add a quick example to the readme using TypeScript. I was experimenting with it myself today and ran into some snags with how the props should be passed defined and passed in, so I'm sure it would be helpful to others too. cc'ing a couple more folks from #219 who might like to contribute a sample to docs: @rjhilgefort, @hburrows, @ipanasenko |
Specifically, the problem I ran into was a bit different than described above, but it seems to be same root cause. For me, the problem is that Here is a simplified example: import React, { Component } from 'react'
import { connect, PromiseState } from 'react-refetch'
import * as types from '../types'
interface Props {
executionId: string
executionFetch: PromiseState<types.WorkflowExecution>
}
class WorkflowState extends Component<Props> {
render() {
// do something with this.props.executionFetch
return null
}
}
export default connect((props: Props) => {
return {
executionFetch: `/executions/${props.executionId}`,
}
})(WorkflowState) Notice how
Keeping
It looks like @yannick-cw is on to something with his fix, but I'm curious how others are using the TypeScript definitions because it seems like you'd quickly run into these problems. Of course, I'm a TypeScript noob and could completely be doing it wrong 🤷♂ |
The problem is still present in
The only thing different is the removal of the This keeps everything working so far without any other changes to our codebase, so his original fix is still compatible after the update. |
@yannick-cw 's fix above has been integrated in #242. PTAL :) |
Nice, thanks @ryanbrainard |
Oh, the PR (#242) isn't merged yet, so re-opening this. @yannick-cw, does the example in the PR work how you were envisioning this be used? |
Oh I misread that, I'll take a look |
@ryanbrainard I think the example looks good 👍 |
Hey, I tried to integrate react-refetch to our stack and could not get it to work with typescript properly. To me it looks like there are some problems with the type definitions, but maybe I missed something on usage side.
Here is a short example
This fails to compile at two places
First at usage side it seems to still require the properties, that are actually added in the connect:
Second the
?
optional on the exampleUpdateDone does not work with the connect, but is required, as theexampleUpdateDone
is optional.If I am missing something please give me a hint, otherwise I'd like to help fix these type definitions.
The text was updated successfully, but these errors were encountered: