Repository navigation
Split modules react and react/addons; exporting Component<P, S, C> class - #3615
Conversation
|
It seems that this doesn't allow use with internal modules (since 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 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. |
|
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, |
I'm not sure I entirely understand the problem with this. Are there any negative side-effects? AFAIK in my example if you did 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.
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
I think this could work but might be a little unwieldy. I assume you mean something like: 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 |
|
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:
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 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. |
|
Honestly the entire ecosystem around react targets external modules there is no use to have a definition based on a global @jbrantly your solution about using a different name for react require like lowercase 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. 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 |
|
@fdecampredon thanks for weighing in! I appreciate it.
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.
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
I actually like to use uppercase
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. |
|
@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 Global vs CommonJs React Addons react vs React Event Attributes Context |
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 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. |
Yup but for user defined component we don't want those handlers.
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. |
Do you have a link for that? |
|
@fdecampredon Thanks! Wow. I totally missed that those had the same contents when I looked at them when making ts-loader. I just assumed
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
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.
I also opened a ticket regarding this item: microsoft/TypeScript#2019
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?
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. |
|
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 |
acf33c2 to
9ce634a
Compare
|
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 @fdecampredon I disagree that |
…ame to DOMAttribtues
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 |
|
@jbrantly—I added the global defs and a |
|
@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 |
|
Thanks! |
Split modules react and react/addons; exporting Component<P, S, C> class
|
It looks like we'll be able to take the 3 duplications down to 2 in TS 1.5: microsoft/TypeScript#2019 |
|
Awesome! |
|
👍 👍 👍 thanks for following up, @jbrantly |
So... this PR splits
react-0.13.0.d.tsandreact-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.Componentdoesn't have type parameters (it has the type interfaceReact.ComponentClass<any, any, any>, which is not extendable by aclass). The reason this seems to work right now is becauseReact.Componentreferences the class in the globalReactmodule, and not in"react"or"react/addons". So if youimport react,react.Componentis not a class.The Solution
Export
class Component<P, S, C>directly frommodule "react"as well as frommodule "react/addons". This has some unfortunate side effects:Top Level Exports
Previously we managed sharing between
"react"and"react/addons"by extendingReact.AddonsExportsfromReact.Exports. Now that we need to export a class, we can't use export assignment:So every Top Level API method must be exported individually.
No Shared Interfaces
We got away with packing all React-related interfaces into the global
Reactmodule because neither"react"nor"react/addons"exported any individual elements. Now that they do, If you useit clobbers the
Reactmodule 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:
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:
staticproperties on modern components (needs type annotations)ClassicComponentClass -> ClassicFactory -> ReactClassicElement -> ClassicComponent)cc @pspeter3, @jbrantly