Skip to content

Split modules react and react/addons; exporting Component<P, S, C> class - #3615

Merged
johnnyreilly merged 3 commits into
DefinitelyTyped:masterfrom
AsanaArchive:react-0.13-fix
Feb 17, 2015
Merged

johnnyreilly merged 3 commits into
DefinitelyTyped:masterfrom
AsanaArchive:react-0.13-fix

Conversation

@vsiao

@vsiao vsiao commented Feb 7, 2015

Copy link
Copy Markdown
Contributor

So... this PR splits react-0.13.0.d.ts and react-addons-0.13.0.d.ts, with no shared code or interfaces. That feels bad, but there's a good reason why:

The Problem

React.Exports.Component doesn't have type parameters (it has the type interface React.ComponentClass<any, any, any>, which is not extendable by a class). The reason this seems to work right now is because React.Component references the class in the global React module, and not in "react" or "react/addons". So if you import react, react.Component is not a class.

The Solution

Export class Component<P, S, C> directly from module "react" as well as from module "react/addons". This has some unfortunate side effects:

Top Level Exports

Previously we managed sharing between "react" and "react/addons" by extending React.AddonsExports from React.Exports. Now that we need to export a class, we can't use export assignment:

var exports: React.Exports;
export = exports;

So every Top Level API method must be exported individually.

export function createClass<P, S, C>(...)
export function createElement<P>(...)
export class Component<P, S, C> { ... }

No Shared Interfaces

We got away with packing all React-related interfaces into the global React module because neither "react" nor "react/addons" exported any individual elements. Now that they do, If you use

import React = require("react");

it clobbers the React module definition and its interfaces are no longer accessible. This means that we need to either (a) define global interfaces (ReactElement, ReactComponent, etc.), which is against DT best practices, or (b) declare interfaces in both "react" and "react/addons" modules.

Attempted Workarounds

module merging:

declare module "react/addons" {
    import React = require("react");
    module React { // <---- (*)
        var addons: { ... };
    }
    export = React;
}

Doesn't work. The module to be merged (*) is ignored completely.

So...

This is the conclusion that I came to. We should split up "react" and "react/addons", making sure that changes stay in sync between the two files.

Bonus features:

  • fixes static properties on modern components (needs type annotations)
  • write better tests for Top-Level API, making sure that they preserve type information (eg. ClassicComponentClass -> ClassicFactory -> ReactClassicElement -> ClassicComponent)

cc @pspeter3, @jbrantly

@jbrantly

jbrantly commented Feb 7, 2015

Copy link
Copy Markdown
Contributor

It seems that this doesn't allow use with internal modules (since declare module React is replaced with declare module "react". In fact, I don't think the old definitions supported internal modules either. I think we should have support for internal modules.

I put together a quick idea that I think works. I put it up at https://gist.github.com/jbrantly/a9db05bad85d131c4a7f

The basic idea is that all of the functionality is available through a global module React (including the exported Component class and top level APIs like createClass). This is a change from the old definitions in that you should now be able to do something like React.createClass() in an internal module. The named module "react" is also declared for external modules which exactly equals the global module. No addon functionality is defined in this file.

A second file is created for the addons. The second file extends the global React module to add the "addons" interface and variable, and "react/addons" is declared. In this way you can either use react.d.ts for both internal and external modules without addons, and if you need addons you instead can use react-addons.d.ts which simply extends react.d.ts. No duplicated code.

@pspeter3

pspeter3 commented Feb 9, 2015

Copy link
Copy Markdown
Contributor

We've tried that before. Some of the problems are that the React module and the imports can collide and secondly that if you have both definitions in your project, require("react"); automatically gives you addons because of module merging. Maybe we can have a global module for react interfaces (ReactApi?) and then have the modules export their own individual things. I honestly wish that react/addons was it's own node module so we could just have a separate typings and they did not have an overlapping set of exports.

@jbrantly

jbrantly commented Feb 9, 2015

Copy link
Copy Markdown
Contributor

the React module and the imports can collide

I'm not sure I entirely understand the problem with this. Are there any negative side-effects? AFAIK in my example if you did import React = require('react') and then used React it's true there is potentially some ambiguity between what React refers to (the imported module or the global module) but in practice it doesn't really matter because they both mean the same thing. If anything I think it would be more confusing if they didn't collide because you could do import react = require('react') and usage of both react and React will compile but only react will actually work. But this is just a side-effect of the recommended approach to supporting both internal and external modules with the same declaration file[1].

Also, to be clear, I don't think this point is relevant to the normal/addons separation but more to the internal/external modules separation. I apologize for muddying the waters a bit in this regard. However, I do think we need to keep internal/external module separation in mind.

if you have both definitions in your project, require("react"); automatically gives you addons because of module merging

That's true and unfortunate. IMO it seems like a worthwhile tradeoff to keep from having repeated declarations, but I could certainly understand if others disagreed. It would be nice if we didn't have to make the tradeoff at all somehow but I don't see another alternative.

Another thing to keep in mind is that it's possible this doesn't actually break anything at run time. Assuming you're using both, as you said, it looks like addons gets appended to the react export so require('react') would actually have the addons assuming require('react/addons') had also been called. But that isn't guaranteed. Just food for thought. See https://github.com/facebook/react/blob/master/src/browser/ReactWithAddons.js

Maybe we can have a global module for react interfaces (ReactApi?) and then have the modules export their own individual things

I think this could work but might be a little unwieldy. I assume you mean something like:

import react = require('react');

var elm: ReactApi.ReactElement<any> = react.createElement(...)

Not entirely sure how I feel about that. I think it might lead to more confusion than is necessary.


What I really don't want to see is the definitions being repeated into a bunch of files. I could foresee repeating in 3 files: one for external modules, one for external modules with addons, and one for internal modules. While that may model everything really well it seems like overkill to me. I think we need to make some concessions. TypeScript Handbook [1] recommends putting both internal and external module definitions in a single file. This has the concession that if you're using external modules, the internal module definitions are still available but the user just has to be aware not to use them. I think we can apply a similar concession to the normal/addons separation. If you're not using addons, fine, don't include that definition file and you don't get it. If you are, then include the right definition file and you get addons everywhere.

[1] "Global / External-agnostic Libraries" http://www.typescriptlang.org/Handbook#writing-dts-files-examples

@jbrantly

jbrantly commented Feb 9, 2015

Copy link
Copy Markdown
Contributor

In the interest of moving forward (I definitely agree that the current state of the definitions is broken), I think it boils down to this:

  1. How are we going to handle internal modules?
    1. We don't. External only.
    2. We do. We need to have a global React module somewhere.
      1. We have a global React module and the external module just exports it (ala my example)
      2. We put the global React module in a completely separate file and duplicate code. (so you need to choose which declaration file to use depending on whether or not you plan on exposing React as a global)
      3. We defer for another PR.
  2. How are we going to handle addons?
    1. We duplicate interfaces. This allows fine-grained control (require('react') will never have the addons property).
    2. We use module merging to not duplicate code (ala my example). In this case if you reference the addons declaration file anywhere in your project then require('react') will include the addons property.

Please note that the original issue (React.Component not being a class) is actually solved more by the internal/external module solution than it is by the addons solution. The addons solution (I believe) only effects whether or not require('react') has the addons property. This PR inadvertently fixes the issue by going external only.

So I think if we each just voted and see where we wind up that mind lead to more fruitful discussion and progress than me making walls of text.

I personally vote for 1.ii.a and 2.ii for the reasons previously outlined.

@fdecampredon

Copy link
Copy Markdown
Contributor

Honestly the entire ecosystem around react targets external modules there is no use to have a definition based on a global React module, as for addons separation I think there is a better solution : just don't provide any declarations for addons, in future react release this will anyway be pulled out of master in separate repo, and for myself I already request thoses addons this way:

var cx = require('react/lib/cx');

@jbrantly your solution about using a different name for react require like lowercase react is really problematic if you want to use jsx-trypescript, because like the officila jsx compiler it converts tags to 'React.createClass'.

In general the definition of React is really wrong in a lots of way, for example: https://github.com/borisyankov/DefinitelyTyped/blob/master/react/react.d.ts#L244-L288 is entirely wrong the events handler should only be on HTML Element, adding a third generic types on component for context have practically no value but is quite bother some, the overload of createElement should not use union type etc etc.
I personally use https://github.com/fdecampredon/jsx-typescript/blob/jsx/typings/react.d.ts and even if it's not as complete it allows me to get a far better type inference.

Finally on the topic of external vs internal module I think the problem is deep, I can't personally use any of the DefinitelyTyped definition files as is, I don't want my global scope to be polluted by anything when I work with external modules, and I hate the fact that some of those file use ///<reference /> when I strictly forbid them from my own project.
This is a build problem I think there would be a way to create a definition for internal module and find something that automatically transform it in a external module def. but that's another topic.

@jbrantly

Copy link
Copy Markdown
Contributor

@fdecampredon thanks for weighing in! I appreciate it.

Honestly the entire ecosystem around react targets external modules there is no use to have a definition based on a global React module

I disagree. React exposes a global object for people to use, I think we should allow for that in the definitions. The React tutorial uses it. The two most popular (I think) 3rd party React components expose globals: react-router and react-bootstrap. While most people may use external modules and for good reason I don't think we should exclude those that don't.

just don't provide any declarations for addons, in future react release this will anyway be pulled out of master in separate repo

That's good news! That is exactly what @pspeter3 was hoping for. I think the issue is that the officially documented way of using addons is react/addons and the addons property, so for now I'm guessing that's what we should (unfortunately) model.

your solution about using a different name for react require like lowercase react is really problematic if you want to use jsx-trypescript, because like the officila jsx compiler it converts tags to 'React.createClass'.

I actually like to use uppercase React for just that purpose, it's the Asana guys that want to use lowercase react. But there's really no reason that they shouldn't be able to do that if they want.

I don't want my global scope to be polluted by anything when I work with external modules

So it sounds like you would be a proponent of putting any internal module definitions (assuming we had them in the first place) in a separate, duplicated file.

@pspeter3

Copy link
Copy Markdown
Contributor

@jbrantly are you now advocating 1.ii.b and 2.i? I generally agree with @fdecampredon that I don't want my global scope touched when I'm using external modules though don't feel strongly about the /// <reference syntax.

Global vs CommonJs
I agree with @fdecampredon. I don't understand where you are getting this exposed React global unless you are directly linking to the script tag. I feel like most people use the npm module for rendering on the server and then use browserify or webpack.

React Addons
Having declarations for the test utilities has been awesome. One solution I had was to define them all as external modules so you just require the exact ones you want. In the long run, a separate module will achieve the same result. I think removing the addons type definitions entirely though would be a shame.

react vs React
We've been using react instead of React purely because IntelliJ becomes confused when you have a local variable React and a global module React. It shouldn't matter at the end of the day but we would prefer to use React.

Event Attributes
The event handlers are only for HTML and SVG elements. I believe SVG elements can also have event handlers.

Context
Adding a third generic is actually fairly helpful for our case. We use context as a means for dependency injection and have a way to ensure that components have sufficient context to create all of their children.

@fdecampredon

Copy link
Copy Markdown
Contributor

So it sounds like you would be a proponent of putting any internal module definitions (assuming we had them in the first place) in a separate, duplicated file.

Not really I think it would be easy to provide a way to automatically build the definitions in the 2 modes for example pretty easily we could wrap a definition written in internal module :

/// internal module def
declare module MyModule {
  export init(): void;
}

would be transformed into :

/// external module def
declare module 'my-module' {
  module MyModule {
    export init(): void
  }
  export = MyModule
}

Alternatively we could have a 'template file' for definition file that can generate both mode:

/// template def
declare module <% module.name %> {
   export function init(): void;
}

That would be transformed into :

/// internal module def
declare module MyModule {
  export init(): void;
}

and

/// external module def
declare module 'my-module' {
  export init(): void
}

But this second solutions could cause problem with export =.

The typescript team pretty much have done something similar since now we have 2 declarations files for typescript one for external module, the other for internal module.

@fdecampredon

Copy link
Copy Markdown
Contributor

Event Attributes
The event handlers are only for HTML and SVG elements. I believe SVG elements can also have event handlers.

Yup but for user defined component we don't want those handlers.

Context
Adding a third generic is actually fairly helpful for our case. We use context as a means for dependency injection and have a way to ensure that components have sufficient context to create all of their children.

I can understand but honestly what's the difference between :

class MyClass extends React.Component<{},{},{ myContextVar: string}> {
}

and

class MyClass extends React.Component<{},{}> {
  context: { myContextVar: string}
}

I doubt that you access to context outside of the component or that you can enforce type-checking on children based on the parent context or getChildContextMethod.
Anyway this is honestly a case that very very few people uses, and I feel that it gets in the way of comprehension and simplicity for people who don't use it.

@jbrantly

Copy link
Copy Markdown
Contributor

The typescript team pretty much have done something similar since now we have 2 declarations files for typescript one for external module, the other for internal module.

Do you have a link for that?

@jbrantly

Copy link
Copy Markdown
Contributor

@fdecampredon Thanks! Wow. I totally missed that those had the same contents when I looked at them when making ts-loader. I just assumed typescriptServices was for the language services instead of the compiler. That is really interesting.

are you now advocating 1.ii.b and 2.i?

I'm not advocating it but I'm willing to concede on the point since everybody else doesn't want to pollute the global namespace when using external modules (which I totally understand). Pollution is the way that TypeScript currently recommends in the Handbook and the way pretty much every other definition file out there does it, and it sucks that we'll have duplicate definitions and need to keep multiple copies in sync, but I understand the end-goal. Apparently the TypeScript team does as well according to the links @fdecampredon provided. It's certainly not the hill I'll die on, but I wanted to make sure we had some discussion around it before giving my +1 since there are definitely tradeoffs involved.

I did open an issue in TypeScript for this: microsoft/TypeScript#2018

I don't understand where you are getting this exposed React global unless you are directly linking to the script tag. I feel like most people use the npm module for rendering on the server and then use browserify or webpack.

That's totally where I'm getting it. Well, not me personally, but where some people can get it. I'm not saying it's a best-practice or even commonly used thing, but it is part of the publicly documented React API and for the x% of people that do use it, however small x is, I think we should support it. Exactly for the same reasons that the two of the most popular 3rd party React components support it.

React Addons

I also opened a ticket regarding this item: microsoft/TypeScript#2019

Not really I think it would be easy to provide a way to automatically build the definitions in the 2 modes for example pretty easily we could wrap a definition written in internal module.

I think that would be great. I have no idea how we would go about doing that in the context of DefinitelyTyped. I don't know if there's precedent for it here. Perhaps we could ask the DT people?

Event attributes, context, etc

I wonder if those items should be fixed/discussed in a separate PR and keep this one just related to the normal/addons issue and possibly the internal/external issue.


To sum up, I'm willing to give this PR a +1 as-is and essentially choose 1.ii.c where we create another PR to add internal module support that looks like 1.ii.b unless @vsiao wants to update this PR to also include it.

@pspeter3 pspeter3 mentioned this pull request Feb 12, 2015
@pspeter3

Copy link
Copy Markdown
Contributor

I agree with this plan. I think we should iterate to add 1.ii.b and at least fix the EventAttributes since @fdecampredon correctly pointed out this is a bug. Thoughts @vsiao?

As for context, @fdecampredon, I've created #3641

@vsiao

vsiao commented Feb 13, 2015

Copy link
Copy Markdown
Contributor Author

Hey there, sorry for going MIA—been busy past couple of days.

@jbrantly Good point on external modules; we should definitely support them somewhere. But are we heading towards 4x code duplication? From what I'm reading, 1.ii.b sounds like it requires react.d.ts, react-addons.d.ts, react-global.d.ts, and react-addons-global.d.ts :( To that end, @pspeter3 and I would like to explore writing a script to generate those files so we don't have to do the code copying every time we make a change.

@fdecampredon I disagree that ReactAttributes is 'entirely' wrong—it is meant only as a base to HTMLAttributes and SVGAttributes; composite component props should not extend from ReactAttributes. We could perhaps take key, ref, and children out as BaseProps as you have, but the problem with having P extends BaseProps everywhere is that key, ref, and children are explicitly removed from the props property of ReactElement. I do however agree with you on contexts; they aren't a core feature so perhaps we can think of a way to support it without over-complexifying the types for simple use-cases.

@jbrantly

Copy link
Copy Markdown
Contributor

But are we heading towards 4x code duplication?

If you look at the issue I opened on TypeScript they are saying that you can merge external modules. So it might actually only be 2x code duplication (one for external modules and one for internal). I haven't gotten it to actually work yet though.

Regarding internal modules, there is really no reason to duplicate code in react-global.d.ts and react-addons-global.d.ts. As soon as react-addons-global.d.ts is referenced anywhere in the project the addons property will be available everywhere. So I think the final version will have either 2x duplication (if we can get that external module merging issue resolved) or 3x.

@vsiao

vsiao commented Feb 14, 2015

Copy link
Copy Markdown
Contributor Author

@jbrantly—I added the global defs and a README.md to explain the duplication and discussion process. So I think that puts this PR squarely in 1.ii.b. lmk if you have any questions or concerns, and cc a collaborator if you think this is merge-ready. Thanks!

@jbrantly

Copy link
Copy Markdown
Contributor

@vsiao Sorry for the delay, I was on vacation. This looks really good! Thanks a ton for all the work you put into this.

@johnnyreilly @vvakame +1 from me

@johnnyreilly

Copy link
Copy Markdown
Member

Thanks!

johnnyreilly added a commit that referenced this pull request Feb 17, 2015
Split modules react and react/addons; exporting Component<P, S, C> class
@johnnyreilly
johnnyreilly merged commit 49b821f into DefinitelyTyped:master Feb 17, 2015
@jbrantly

Copy link
Copy Markdown
Contributor

It looks like we'll be able to take the 3 duplications down to 2 in TS 1.5: microsoft/TypeScript#2019

@pspeter3

Copy link
Copy Markdown
Contributor

Awesome!

@vsiao
vsiao deleted the react-0.13-fix branch February 23, 2015 21:32
@vsiao

vsiao commented Feb 23, 2015

Copy link
Copy Markdown
Contributor Author

👍 👍 👍 thanks for following up, @jbrantly

@ngbrown

ngbrown commented Sep 3, 2015

Copy link
Copy Markdown
Contributor

@vsiao, I have been able to merge them with the ES6 exports in TypeScript 1.5. See merge request #5660 and comment there.

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.

6 participants