Skip to content

[react-router] Improve withRouter return type#21329

Merged
johnnyreilly merged 3 commits into
DefinitelyTyped:masterfrom
Grmiade:feature/grmiade/react-router-dom
Nov 23, 2017
Merged

[react-router] Improve withRouter return type#21329
johnnyreilly merged 3 commits into
DefinitelyTyped:masterfrom
Grmiade:feature/grmiade/react-router-dom

Conversation

@Grmiade

@Grmiade Grmiade commented Nov 8, 2017

Copy link
Copy Markdown
Contributor

Related to #17181

This PR allow to improve the withRouter type return by omitting RouteComponentProps.
No need to specify props type of the returned component. It's inferenced now =)

Decorator type has same problem :/
But I didn't find a solution for the moment. Any idea?

@Grmiade Grmiade requested a review from johnnyreilly as a code owner November 8, 2017 00:02
@dt-bot

dt-bot commented Nov 8, 2017

Copy link
Copy Markdown
Member

types/react-router/index.d.ts

to authors (@sergey-buturlakin @mrk21 @vasek17 @ngbrown @awendland @KostyaEsmukov @johnnyreilly @LKay @DovydasNavickas @tkrotoff @huy-nguyen @Grmiade @DaIgeb @egorshulga). Could you review this PR?
👍 or 👎?

@bilby91

bilby91 commented Nov 8, 2017

Copy link
Copy Markdown

Looking forward on this!

@RyanCavanaugh RyanCavanaugh added Popular package This PR affects a popular package (as counted by NPM download counts). The Travis CI build failed labels Nov 8, 2017
@RyanCavanaugh

Copy link
Copy Markdown
Member

@Grmiade Please fix the failures indicated in the Travis CI log.

@typescript-bot

Copy link
Copy Markdown
Contributor

@Grmiade Please fix the failures indicated in the Travis CI log.

@smattt

smattt commented Nov 20, 2017

Copy link
Copy Markdown

Anxiously awaiting this change. Thanks for pushing this along!

@Grmiade Grmiade force-pushed the feature/grmiade/react-router-dom branch from 812e1ab to 83ba2b8 Compare November 20, 2017 16:38
@Grmiade Grmiade force-pushed the feature/grmiade/react-router-dom branch from 83ba2b8 to fb95a9b Compare November 20, 2017 16:42
@Grmiade Grmiade force-pushed the feature/grmiade/react-router-dom branch from a43aab4 to 2689bb2 Compare November 20, 2017 16:54
@Grmiade

Grmiade commented Nov 20, 2017

Copy link
Copy Markdown
Contributor Author

I had to remove WithRouterDecorator.tsx test :/ And update to Typescript 2.4
There is a known issue in TypeScript, which doesn't allow decorators to change the signature of the classes they are decorating. I left a comment to specify this.

@bilby91

bilby91 commented Nov 23, 2017

Copy link
Copy Markdown

Any ETA for getting this merged ?

@johnnyreilly johnnyreilly merged commit 3289762 into DefinitelyTyped:master Nov 23, 2017
@bilby91

bilby91 commented Nov 23, 2017

Copy link
Copy Markdown

@johnnyreilly Newbie question here. How much time it take to get the this change in npm ? Is it done automatically ?

@johnnyreilly

Copy link
Copy Markdown
Member

It's an automated process @bilby91 - should appear in the next 24 hours or so. ( Occasionally it fouls up but AFAIK that's rare)

@bilby91

bilby91 commented Nov 23, 2017

Copy link
Copy Markdown

Good! Will wait then!

Thanks for the info BTW.


export function matchPath<P>(pathname: string, props: RouteProps): match<P> | null;
export function withRouter<P>(component: React.ComponentType<RouteComponentProps<any> & P>): React.ComponentClass<P>;
export function withRouter<P extends RouteComponentProps<any>>(component: React.ComponentType<P>): React.ComponentClass<Omit<P, keyof RouteComponentProps<any>>>;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have ran into an issue with this change and would be interested to get your thoughts @Grmiade: #24070

Separately, do you know why the old type definition didn't work? It seems like it should. If you provide the generic explicitly, it works as expected. Perhaps it's a bug with TypeScript's generic inference? I filed a TypeScript bug here: microsoft/TypeScript#21964

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We wanted to fix this issue #17181
And make the function withRouter smarter. No need to provide explicit type. It can be inferred.
The typings becomes more safe. But indeed, you found a bug with the Omit definition.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were also hitting #17181. Thanks for fixing it!

I'm still curious why the old definition didn't work. I would have expected the return type to omit the router props, because the generic excludes the router props. Is that what you would expected? However, it seems TypeScript is not inferring the generic to exclude the router props. I filed a TS bug for that here—please chime in! microsoft/TypeScript#21964.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interface TOwnProps extends RouteComponentProps<any> {
    username: string;
}

const ComponentFunction = (props: TOwnProps) => (
    <h2>Welcome {props.username} on {props.location.pathname}</h2>
);

const WithRouterComponentFunction = withRouter(ComponentFunction);
// withRouter<TOwnProps>(component: React.ComponentType<RouteComponentProps<any> & TOwnProps>): React.ComponentClass<TOwnProps>

#17181 issue appears when TOwnProps already includes RouteComponentProps because the returned component includes RouteComponentProps too.

Indeed Typescript is not inferring the generic to exclude the router props. Bug or miss-feature, I don't know :/

@1999

1999 commented May 31, 2018

Copy link
Copy Markdown
Contributor

The problem with DefinitelyTyped definitions and PRs like this is missing examples. I actually tried to use withRouter(ReactComponent) in typescript but I can't understand what I need to pass as a generic 🤦‍♂️

@Grmiade

Grmiade commented Jun 1, 2018

Copy link
Copy Markdown
Contributor Author

@1999 You can find so many examples in the withRouter test file. You will see the generic type is not needed, it should be smart and inferred in so many case ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Popular package This PR affects a popular package (as counted by NPM download counts).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants