Skip to content

Bring react and react-addons together to make it easier for other libraries. - #5660

Closed
ngbrown wants to merge 6 commits into
DefinitelyTyped:masterfrom
ngbrown-forks:patch-7
Closed

ngbrown wants to merge 6 commits into
DefinitelyTyped:masterfrom
ngbrown-forks:patch-7

Conversation

@ngbrown

@ngbrown ngbrown commented Sep 3, 2015

Copy link
Copy Markdown
Contributor

Bring react and react-addons together to make it easier for other libraries to depend on just 'react' and the user can optionally use require('react/addons') without conflict. Uses ES6 modules and re-exports to accomplish this.

This has a breaking change in that the additional types in react-addons are now under the .addons. module. For example, where it used to be React.LinkedStateMixin it now is React.addons.LinkedStateMixin. This is only true for the interface definition. The actual mixin variable was already React.addons.LinkedStateMixin.

@ngbrown

ngbrown commented Sep 3, 2015

Copy link
Copy Markdown
Contributor Author

This affects the work done by @DanielRosenwasser and @RyanCavanaugh among others on react; @mrk21 and @vvakame among others in react-router; @stepancar in react-swf; and @pspeter3 and @mrk21 on fluxxor. Please take a look at these changes and review.

I have new library definitions for material-ui, redux, and react-mixin that are built on these changes. The library only requires the base 'react' definitions, while my application is free to chose the 'react/addon' in files I want to use a mixin.

Comment thread fluxxor/fluxxor.d.ts Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If you don't use the import inside the module declaration, I believe you can just name it React

@DanielRosenwasser

Copy link
Copy Markdown
Member

Hey @ngbrown, I'll try to give it a try if I get the chance tomorrow. I'm not as familiar with React as @RyanCavanaugh or others are, but most of my changes related to preparing the definition files for TypeScript 1.6.

@jbrantly

jbrantly commented Sep 3, 2015

Copy link
Copy Markdown
Contributor

See also #5221 and #5579 which is a similar restructure.

I haven't dived super deep into this yet but I do have a couple comments.

  1. While this removes duplication between "react" and "react/addons", it does not remove duplication between react-global.d.ts and react.d.ts. In React v0.14 (releasing any day now I think) "react/addons" doesn't exist anymore and therefore there will be nothing to duplicate there.
  2. The JSX namespace is nested under the "react" module. Does that actually work? I was under the impression that a namespace declared under a module would be scoped to that module, and that TS only looked for the JSX namespace from the global root. I haven't actually tried it though.

@ngbrown

ngbrown commented Sep 3, 2015

Copy link
Copy Markdown
Contributor Author

@jbrantly, The JSX namespace thing is a good question. I'm looking more into that, so far I haven't been able to get typescript to fail for any good or bad intrinsic elements like I would expect them to per the wiki page, even with a global JSX.IntrinsicElements.

This isn't just to remove duplication. It is to let me add more React library typings without a conflict with the __React vs "react/addons" modules and the duplication that was resulting if I tried to fully synchronize them including the JSX module. Because the JSX module wasn't in "react-addons.d.ts", the render() function wasn't on the React.Component, and then the JSX compilation would fail. Your solution in #5221 is to combine everything into one file, but that didn't eliminate any duplication and makes it harder to duplicate changes via a diff tool.

@jbrantly

jbrantly commented Sep 3, 2015

Copy link
Copy Markdown
Contributor

@ngbrown I guess my point is that there isn't a huge need right now to eliminate the duplication between "react" and "react/addons" because "react/addons" is going away very soon. It's also not possible to eliminate the duplication between "react" and "react/addons" while also eliminating the duplication between the namespace React and the module(s). In my mind it's much more important to eliminate the duplication between those.

Because the JSX module wasn't in "react-addons.d.ts", the render() function wasn't on the React.Component, and then the JSX compilation would fail.

I think the real solution here would be for react-addons.d.ts to use /// <reference path="react.d.ts" />. Then the JSX namespace would be available and your change here could have included the necessary render() function in react-addons.d.ts.

@ngbrown

ngbrown commented Sep 4, 2015

Copy link
Copy Markdown
Contributor Author

After much experimentation, it seems the most stable and error free approach is to reference react-global.d.ts from react.d.ts and use its global JSX namespace. Since TSX requires the import to be named React, there shouldn't be any confusion.

I tried #5579 and am getting an error with multiple exports when an export = is used so I would like to continue to this pull request.

@jbrantly

jbrantly commented Sep 4, 2015

Copy link
Copy Markdown
Contributor

@ngbrown The issue with referencing react-global.d.ts (vs just react.d.ts) is that react-global.d.ts exposes the React namespace. A large part of the point of separating the definitions into global vs module was so that would not happen.

As an example, the following would be fine at compile time but would error at runtime:

/// <reference path="react-addons.d.ts" />

// note: there is no `import` statement here

React.render(React.createElement('div'))

Make react-addons depend on react-addons-global so it easy to pull the right definitions with just a tsd install react-addons, whether you are using React globally or not.
@ngbrown

ngbrown commented Sep 4, 2015

Copy link
Copy Markdown
Contributor Author

@jbrantly I think that if the developer is using import statements, they would not be using /// <reference /> statements. I don't think other library definitions try to keep things separate. For example, the angular definitions have global ng and angular namespaces along with the "angular" module.

In general, I don't think that most developers would expect to have to choose between a react or a react-global when pulling from DefinitelyTyped via tsd or a NuGet package. With this approach, they will get all the files they need with just react or react-addons.

I still feel this approach is the best of both worlds in that it is;

  • easy to diff between react.d.ts and react-global.d.ts as well as react-addons.d.ts and react-addons-global.d.ts to compare the only things that should be synchronized
  • react-addons.d.ts is purely additive
  • everything just works as expected when the developer pulls react or react-addons.

The downside of having it compile when wrongly using reference instead of import doesn't seem to bother other library definitions.

@jbrantly

jbrantly commented Sep 4, 2015

Copy link
Copy Markdown
Contributor

I think that if the developer is using import statements, they would not be using /// statements

I'm saying that the developer is supposed to be using modules with a module bundler, but accidentally forgot to put the import statement. Ideally there would be a compile-time error but there won't be because TypeScript thinks the global React namespace is available even though at runtime it's really not. The /// <reference /> part is just a quick way to show that a particular definition file is pulled into the program without saying "this definition file is specified in the tsconfig.json" or something.

I don't think other library definitions try to keep things separate. For example, the angular definitions have global ng and angular namespaces along with the "angular" module.

You're right, most don't, and I think that's a problem which is why I've created issues like microsoft/TypeScript#2018 microsoft/TypeScript#4337. The problem is that it's really convenient to do it the current way (you provide both namespace and module versions without repeating code), so that's what everyone does. We discussed this extensively in #3615 for the React typings and decided on splitting them out. FWIW, TypeScript itself provides separate files to its own API for modules vs namespace. It's just a PITA that TS needs to address, and it's only going to get worse as ES6 modules become more popular.

In any case, regarding this PR (since I've digressed a bit, sorry), I think that the main issue it's trying to address (no duplication between "react" and "react/addons") is a non-concern at this point since addons are going away very soon. It also leaves duplication between the module and namespace versions, whereas #5221 will have no duplication at the cost of the weird __React namespace pollution which is necessary for JSX anyways.

I think if this came down the pipe a few months ago it would make more sense, but at this juncture probably not. Just my 2 cents.

@alihammad-gist

Copy link
Copy Markdown

I am getting this error

var e1: React.ReactElement<any> = React.DOM.li();
var e2: ReactWithAddons.ReactElement<any> = ReactWithAddons.DOM.li();
e1 = e2;  // produces error
// Error:(11, 1) TS2322: Type 'ReactElement<any>' is not assignable to type 'ReactElement<any>'.

They should be structurally similar but i am getting this error

@johnnyreilly

Copy link
Copy Markdown
Member

I'm about to attempt a port which attempts to plug together TypeScript using ES6 emit, React, Browserify and Babel and ideally I'd like this to be in place first.

Are we ready to merge this now - or are there any remaining concerns? If everyone is happy I'm happy to proceed with the merge.

@jbrantly

jbrantly commented Sep 7, 2015

Copy link
Copy Markdown
Contributor

@johnnyreilly I hope this isn't rude but did you see #5579?

I still have the concern of everything now pulling in the global React namespace. That was something that @vsiao and @pspeter3 and I had explicitly tried to avoid. However, I completely understand that most existing definitions don't do what we do, so when in Rome.

On a side note, I somewhat recently did the same thing you are about to do. I wrote up a blog post that might be helpful to you since you're using ES6 modules + Babel. For the React definitions, all I did was change

declare module "react" {
    export = __React;
}

to

declare module "react" {
    export default __React;
}

And then use import React from 'react' syntax. If you want to use import { Component } from 'react' or something then you'd need to do more work (blog post has details).

@johnnyreilly

Copy link
Copy Markdown
Member

Hi @jbrantly,

Sorry no I didn't see #5579 - I've left a comment on there to try and prompt other people on the thread. If there's no objection then hopefully we can merge it.

Thanks for sharing the blog post by the way. I haven't had a chance to read it yet but it looks like you've been trying to solve a similar problem to me but using webpack rather than browserify? In which case that could be very useful. 👍 Thanks again!

@johnnyreilly

Copy link
Copy Markdown
Member

Hi @ngbrown,

I just wanted to alert you that the merging of #5579 means that this PR needs some further work. Sorry about that!

@ngbrown

ngbrown commented Sep 8, 2015

Copy link
Copy Markdown
Contributor Author

#5579 supersedes this. Closing. Possible follow-on pull request promoting types into the addons namespace.

@ngbrown ngbrown closed this Sep 8, 2015
@ngbrown
ngbrown deleted the patch-7 branch September 8, 2015 15:54
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.

5 participants