Repository navigation
Description
I've been bouncing some ideas around in my head and wanted to get some discussion going. In #3615 we split the definitions into 4 files with duplicated declarations in 3 of those files. Ideally we'd like to have the same (or similar) functionality but without duplicated declarations.
react.d.ts is already creating the namespace __React for compatibility with JSX. I think we can use that concept to get the duplicated declarations down to 2 right now and when React v0.14 releases get that down to just 1. We can also (possibly) reduce the number of files down to just 2.
// ----react.d.ts----
declare namespace __React {
export ... // all of the declarations
}
declare module "react" {
export = __React;
}
declare module "react/addons" {
export ... // declare addons and *redeclare* all of the declarations, unfortunately no good way to merge in
}
// ----react-namespace.d.ts----
/// <reference path="react.d.ts" />
declare var React: typeof __React;
declare module _React {
// declare addons
}So some notes here:
For the "module" case, you just reference react.d.ts and you get access to both react and react/addons modules (and they're correct, .addons isn't available in react).
For the "namespace" case, you reference react-namespace.d.ts (react.d.ts is automatically pulled in) and you get a global React variable with .addons included. I could see some debate on whether or not to put addons in react-namespace.d.ts vs making it a separate file, but I'm shooting for ease of use here. Either way the end-user has to make sure the runtime and the declarations are in sync.
So with this we would have 2 (or possibly 3) files and only 2 "copies" of the meat of the declarations, down from the current 3. When React v0.14 releases (hopefully soon), react/addons is going away which should make our lives considerably easier and we can simply remove those redeclarations.
I'd like some discussion before putting in the work on making a PR. Thoughts @pspeter3 @vsiao ?
Activity
jbrantly commentedon Aug 4, 2015
Well crap, after posting this I had a thought to test something and it didn't work, so my proposal above is pretty much bunk.
In particular,
declare var React: typeof __Reactdoesn't quite work like I originally thought it did. You can call exported functions easily enough but you can't extend an exported class. So this won't work unless there is some way to easily "rename"__ReacttoReact.jbrantly commentedon Aug 5, 2015
This might be back on the table. After some more research I found that you can rename using the following syntax:
This seems to work globally if in a declaration file. So my original proposal still stands.
vvakame commentedon Aug 8, 2015
and @RyanCavanaugh
pspeter3 commentedon Aug 8, 2015
I think this sounds good to me. I also think the addons problem will go away since you will be able to individually require them
ngbrown commentedon Sep 3, 2015
@jbrantly I tried your branch out, and I ran into similar errors I was getting when I was trying to find a my own independant solution.
While the DefinitelyTyped Test Runner doesn't error, I get the following error when copying these definitions into my project and compiling in webpack (with TypeScript 1.6-beta):
This error also shows in Atom with atom-typescript. The error goes away when the second
declare module "react/addons"is removed. It seems that TypeScript doesn't like both definitions in there. But why does the Test Runner not complain?ngbrown commentedon Sep 4, 2015
@jbrantly I've now seen your issues microsoft/TypeScript#4336 and microsoft/TypeScript#4337 and It's clear that the interaction between namespaces and external modules could really use some more language support. I understand that
export =is supposed to be depreciated, but there's not a good replacement for definition files.The reason I'm getting the TS2309 error on your approach appears to be that TypeScript/compiler/checker.ts/checkExternalModuleExports() errors on the
export =if there are any more than oneexporttotal in the container up to two levels up (which is the file).per @ahejlsberg, on Microsoft/Typescript#2242:
So maybe the compiler is conflating a module with a file? I don't know why it doesn't happen on the Test Runner, but maybe it's because it's ran on individual files and not on a
tsconfig.json? I can't find in the language specification that it should be an error, so maybe it's worth a bug on TypeScript's part. Have you seen this error on your projects?jbrantly commentedon Sep 4, 2015
@ngbrown I think something else is going on with your project where you accidentally have multiple declarations of "react" or "react/addons" or something. I have so far been unable to duplicate what you're seeing. You mentioned bundling with webpack, so I updated my test here with the new bindings and tried to break things (including react-namespace.d.ts, importing "react" vs "react/addons", etc) all using TS 1.6 beta. If you can reproduce (either with a PR to that test or some other minimal test) that would probably be very helpful.
ngbrown commentedon Sep 4, 2015
@jbrantly ok. I figured it out.
It's not a problem with webpack, and I can replicate it with just the TypeScript compiler. I had been testing your changes with my modified react-router definitions. Never once did it complain about the react-router file, but I can replicate it with a simple file.
Drop the following
react-based-component.d.tsinto your test directory and add it to thetsconfig.jsonand you should get the error when runningtsc.Alternately clone this: https://github.com/ngbrown/tsx-test and run
npm installandnode node_modules\typescript\bin\tsc.So ya. Sorry for the confusion.
jbrantly commentedon Sep 4, 2015
With the changes proposed here, the proper way to extend react definitions would be to use the
__Reactnamespace. That way any extensions are automatically available in both the module and namespace version. So to rewrite your definition file:I'm also a little confused as to why you'd need to extend
__Reacthere though. Offhand it doesn't seem like you would need to. I'm wondering if the issue is that you have DefaultComponent extending ReactElement instead of Component. You might also need to make it a class instead of an interface.jbrantly commentedon Sep 4, 2015
To further my comment, I feel like the following definition file would be sufficient (but maybe not):
ngbrown commentedon Sep 4, 2015
That's just the way that react-router was. For my other pull request, I had moved all the react definitions directly into modules, so to make react-router work with that, I was simply changing from namespace to module extensions. Obviously the two approaches conflict with each other and the compiler never told me where the conflict was coming from.
orta commentedon Jun 7, 2021
Hi thread, we're moving DefinitelyTyped to use GitHub Discussions for conversations the
@typesmodules in DefinitelyTyped.To help with the transition, we're closing all issues which haven't had activity in the last 6 months, which includes this issue. If you think closing this issue is a mistake, please pop into the TypeScript Community Discord and mention the issue in the
definitely-typedchannel.