Skip to content

Typing for React Router 0.13 - #4173

Closed
duncanmak wants to merge 2 commits into
DefinitelyTyped:masterfrom
duncanmak:react-router-0.13
Closed

duncanmak wants to merge 2 commits into
DefinitelyTyped:masterfrom
duncanmak:react-router-0.13

Conversation

@duncanmak

Copy link
Copy Markdown
Contributor

This is a new typing for React Router 0.13.

The older copy has been moved to the legacy folder.

Whenever the React typings get updated to include context type information, this typing needs to be updated to reflect that change. Currently, Router.Context can only be used via casting, an example can be found inside react-router-test.ts.

@vvakame

vvakame commented Apr 22, 2015

Copy link
Copy Markdown
Member

Comment thread react/react.d.ts Outdated

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.

Please don't do this. react-global.d.ts is provided for this purpose.

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.

How do I reference react-global.d.ts? Do I just need to include it as part of my build?

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.

I fixed the PR.

What's the thinking behind having multiple .d.ts files?

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.

  1. Declaring a global React module means that React may be referenced even when it hasn't been explicitly imported.
  2. IntelliJ doesn't like it (I don't personally use IntelliJ with react/typescript, but see React: Added Ambient Module Declaration alongside Ambient External Module Declaration #3959 for more context)

@duncanmak

Copy link
Copy Markdown
Contributor Author

I took out the commit that exported the React module and fixed up the legacy file paths.

@duncanmak
duncanmak force-pushed the react-router-0.13 branch 4 times, most recently from b40a61b to 8a6b813 Compare April 23, 2015 02:55
@duncanmak

Copy link
Copy Markdown
Contributor Author

I cleaned up the patches and we're down to one error:
react-router/react-router-test.ts(71,10): error TS2339: Property 'contextTypes' does not exist on type 'typeof Messages'.

I don't think this can be avoided, perhaps @vsiao has some ideas for what we can do here.

@vsiao

vsiao commented Apr 23, 2015

Copy link
Copy Markdown
Contributor

Do you absolutely need to use context? It's not officially supported so I haven't fleshed out the types for it yet. Try setting contextTypes as a static property on Messages?

@duncanmak

Copy link
Copy Markdown
Contributor Author

@vsiao Adding a static property breaks compilation - when I add that in, I get:

2345 Argument of type 'typeof Messages' is not assignable to parameter of type 'ComponentClass<any>'..

Here's a component definition in full:

class Messages extends React.Component<any, any> {
    context: { router: Router.Context };
    static contextTypes  = { router: React.PropTypes.func };
    DisplayName = 'Messages';
    render = () => DOM.p ({}, this.context.router.getCurrentParams().messageId);
}

This is most definitely related to #3641.

I haven't figured out how React Router uses the contextTypes definition to inject the router yet - either way, without it, I get this error at runtime:

test.ts:61 Uncaught TypeError: Cannot read property 'getCurrentParams' of undefined

Note: The code in the PR right mirrors the approach shown in the React Router docs - https://github.com/rackt/react-router/blob/master/UPGRADE_GUIDE.md

@vsiao

vsiao commented Apr 23, 2015

Copy link
Copy Markdown
Contributor

You'll need to type-annotate contextTypes with React.ValidationMap<Context> where Context is an interface that you define. The type errors given aren't very helpful at the moment, sorry about that. See https://github.com/borisyankov/DefinitelyTyped/blob/master/react/react-tests.ts#L77-79 for an example.

@duncanmak

Copy link
Copy Markdown
Contributor Author

Thanks for working with me through this, @vsiao.

One difference now that I'm using react-global.d.ts - I don't have import React = require('react') at the top anymore, and with that, even though the TS -> JS compilation works, I see this error at runtime:

Uncaught ReferenceError: React is not defined.

@duncanmak

Copy link
Copy Markdown
Contributor Author

Other than the error above, if I revert back to using an exported version of react.d.ts, the following component definition does work:

class Messages extends React.Component<any, any> {
    static contextTypes: React.ValidationMap<Router.Context> = {
        router: React.PropTypes.func
    }

    DisplayName = 'Messages';
    render = () => DOM.p ({}, this.context.router.getCurrentParams().messageId);
}

So, yay, progress!

@duncanmak

Copy link
Copy Markdown
Contributor Author

Have we abandoned this pull request?

@vvakame

vvakame commented Jun 7, 2015

Copy link
Copy Markdown
Member

@vsiao can i merge this PR?
@duncanmak please resolve merge conflict.

@vsiao

vsiao commented Jun 7, 2015

Copy link
Copy Markdown
Contributor

I'd like @pspeter3 to take a look since he knows more about React Router :)

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.

@pspeter3

pspeter3 commented Jun 8, 2015

Copy link
Copy Markdown
Contributor

I agree with @vsiao's comments in the code. I'm also interested why React.createElement needed to be overridden.

@vvakame vvakame closed this Jul 14, 2015
@vvakame

vvakame commented Jul 14, 2015

Copy link
Copy Markdown
Member

I merged #4898 .

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants