Skip to content

Possible React reorganization #5221

Closed
Closed

Description

@jbrantly

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

  1. jbrantly commented on Aug 4, 2015

    @jbrantly
    ContributorAuthor

    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 __React doesn'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" __React to React.

  2. jbrantly commented on Aug 5, 2015

    @jbrantly
    ContributorAuthor

    This might be back on the table. After some more research I found that you can rename using the following syntax:

    import React = __React;
    

    This seems to work globally if in a declaration file. So my original proposal still stands.

  3. vvakame commented on Aug 8, 2015

    @vvakame
    Member
  4. pspeter3 commented on Aug 8, 2015

    @pspeter3
    Contributor

    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

  5. ngbrown commented on Sep 3, 2015

    @ngbrown
    Contributor

    @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):

    ERROR in C:\dev\H-D\VTS\Vts.Passon\web\typings\react\react.d.ts
    (800,5): error TS2309: An export assignment cannot be used in a module with othe
    r exported elements.
    

    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?

  6. ngbrown commented on Sep 4, 2015

    @ngbrown
    Contributor

    @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 one export total in the container up to two levels up (which is the file).

    per @ahejlsberg, on Microsoft/Typescript#2242:

    An export = declaration, which substitutes a different entity to be exported in place of the module itself, is always emitted as an assignment to module.exports. It is an error to have other exports in a module that uses export =. This is the existing TypeScript behavior.

    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?

  7. jbrantly commented on Sep 4, 2015

    @jbrantly
    ContributorAuthor

    @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.

  8. ngbrown commented on Sep 4, 2015

    @ngbrown
    Contributor

    @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.ts into your test directory and add it to the tsconfig.json and you should get the error when running tsc.

    // Type definitions for a random component
    
    ///<reference path='react.d.ts' />
    
    declare module "react-based-component" {
      import * as React from "react";
    
      interface DefaultComponentProp {
          name?: string;
          handler: React.ComponentClass<any>;
      }
    
      interface DefaultComponent extends React.ReactElement<DefaultComponentProp> {}
      interface DefaultComponentClass extends React.ComponentClass<DefaultComponentProp> {}
    }
    
    // I also add to the react namespace
    
    declare module "react" {
      import * as __React from "react";
      import * as Component from "react-based-component";
    
      // for DefaultRoute
      function createElement(
        type: Component.DefaultComponentClass,
        props: Component.DefaultComponentClass,
        ...children: __React.ReactNode[]): Component.DefaultComponent;
    }

    Alternately clone this: https://github.com/ngbrown/tsx-test and run npm install and node node_modules\typescript\bin\tsc.

    So ya. Sorry for the confusion.

  9. jbrantly commented on Sep 4, 2015

    @jbrantly
    ContributorAuthor

    With the changes proposed here, the proper way to extend react definitions would be to use the __React namespace. That way any extensions are automatically available in both the module and namespace version. So to rewrite your definition file:

    ///<reference path='react.d.ts' />
    declare namespace __ReactBasedComponent {
      interface DefaultComponentProp {
          name?: string;
          handler: __React.ComponentClass<any>;
      }
      interface DefaultComponent extends __React.ReactElement<DefaultComponentProp> {}
      interface DefaultComponentClass extends __React.ComponentClass<DefaultComponentProp> {}
    }
    
    declare module "react-based-component" {
      export = __ReactBasedComponent;
    }
    
    declare namespace __React {
      // for DefaultRoute
      function createElement(
        type: __ReactBasedComponent.DefaultComponentClass,
        props: __ReactBasedComponent.DefaultComponentClass,
        ...children: __React.ReactNode[]): __ReactBasedComponent.DefaultComponent;
    }

    I'm also a little confused as to why you'd need to extend __React here 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.

  10. jbrantly commented on Sep 4, 2015

    @jbrantly
    ContributorAuthor

    To further my comment, I feel like the following definition file would be sufficient (but maybe not):

    ///<reference path='react.d.ts' />
    
    declare module "react-based-component" {
      export interface DefaultComponentProp {
          name?: string;
          handler: __React.ComponentClass<any>;
      }
      export class DefaultComponent extends __React.Component<DefaultComponentProp, any> {}
    }
  11. ngbrown commented on Sep 4, 2015

    @ngbrown
    Contributor

    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.

  12. orta commented on Jun 7, 2021

    @orta
    Collaborator

    Hi thread, we're moving DefinitelyTyped to use GitHub Discussions for conversations the @types modules 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-typed channel.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions