Skip to content

Typescript Recipes #46

Closed
Closed

Description

Add some recipes for typescript, including concepts such as *AwareState

Activity

  1. navneet-g commented on Jan 19, 2019

    @navneet-g
    Contributor
  2. abettadapur commented on Jan 19, 2019

    @abettadapur
    CollaboratorAuthor
  3. abdurahmanus commented on Jan 21, 2019

    @abdurahmanus

    I mentioned some problems with typings in this issue: #36
    Imagine I have 2 modules:
    module1:
    image
    image
    and module2 which depends on module1:
    image
    Module2AwareState extends Module1AwareState (as in official typescript-example)
    image
    and I get this error from ts:
    image
    I've also tried to use IModuleTuple interface as a return type of getModule2s function, but in this case I've lost ts benefits
    image

  4. abdurahmanus commented on Jan 21, 2019

    @abdurahmanus

    of course getModule2s from example above should return module with id "module2"

  5. abdurahmanus commented on Jan 21, 2019

    @abdurahmanus

    also I could create temp valiable with type IModule, but reducerMap still wants from me reducer for module1 key.
    image

  6. abdurahmanus commented on Jan 21, 2019

    @abdurahmanus

    another unclear point. DynamicModuleLoader's modules prop type is array of IModuleTuple:
    image
    Does it mean what I aways should wrap getModuleXXX in array when I pass it to DynamicModuleLoader component even if it already returns array of modules like this?
    image
    Or maybe something wrong with typings?

  7. navneet-g commented on Jan 22, 2019

    @navneet-g
    Contributor

    Alex Bettadapur (@abettadapur) can you respond to the IModuleTuple part of the question?
    Regarding the typing issue with reducer, recently we also experience it and follow this pattern, we should update the example to cover this.

    interface IModule1AwareState {
    key1: string;
    }
    
    interface IModule2OwnState {
       key2: string;
    }
    
    interface IModule2AwareState extends IModule2OwnState, IModule1AwareState {
       
    }
    
    const _module2: IModule<IModule2OwnState> = {
    ...
    }
    
    // We just use implicit typing for getModule2 method.
    function getModule2() {
    return [
    getModule1(),
    _module2
    ]
    }
    
  8. danrfg commented on Jan 23, 2019

    @danrfg

    I've been having trouble with typings: The IModuleTuple problem above and using getState() to return a typed object rather than "any". I'm considering creating a getState<IMyModuleState>()

    function that can return the strongly typed module state... is something like this in the works at all?

  9. abettadapur commented on Jan 24, 2019

    @abettadapur
    CollaboratorAuthor

    If anyone has better ideas on ways to implement the typings here, please feel free to let me know, or submit a PR. The main issue I ran into was that it is very difficult to define accurate typings for a dynamic list of items.

    The solution we landed on is what Navneet Gupta (@navneet-g) outlined above, which is to define individual modules separately with a type annotation and return an untyped list of modules, which is ducktyped to IModuleTuple. I didn't intend for IModuleTuple to be used explicitly, it is rather a way to define modules as follows

    const _module1: IModule<IModule1OwnState> = {...};
    const _module2: IModule<IModule2OwnState> = {...};
    
    export const OneAndTwoModule = [_module1, module2];
    
    const _module3: IModule<IModule3OwnState> = {...};
    
    // nested arrays, but handled by the library and transparent to you
    // states that ThreeModule has a dependency on OneAndTwoModule, without directly knowing the exact contents of that module
    export const ThreeModule = [OneAndTwoModule, _module3]; 

    Does that make sense?

    Confusion like this is exactly why I want to add this page. I am drafting it now

  10. abdurahmanus commented on Jan 24, 2019

    @abdurahmanus

    Thank you! The solution with *OwnState and AwareState types works in my case.
    One part what I don't quite understand is wrapping get
    Modules() function (which returns array of modules) in array like this:
    <DynamicModuleLoader modules={[get*Modules()]}>...</DynamicModuleLoader>
    Сan it affect performance? Because as far as I understand each render call we pass new array as 'modules' prop to DynamicModuleLoader comp.
    One more thing. It's very cool that you added a Typescript section to your docs. This section describes Module Dependencies concept which is more general and not directly related to ts. Maybe it would be better to put it in a separate section?
    Thanks again!

  11. danrfg commented on Jan 24, 2019

    @danrfg

    The confusion I have is in the examples. There is createStore<State> that returns an IModuleStore<State> which I guess helps you ensure modules will conform whatever State is, which I suppose would be the module schema e.g. id, reducerMap, initialActions etc.

    Then there's the IModule<State> where State is whatever the reducer for that module returns e.g. IModule<IUserState>.

    The problem comes from store.getState() to grab a particular module for testing. And as they're all typed as any in the DynamicModuleLoader, is there any need to strongly type any IModule?
    I feel like I'm missing something with regards to implementation.

  12. danrfg commented on Jan 24, 2019

    @danrfg

    Btw I wrapped createStore in my own createStore function that has a getModuleById<T>(moduleId:string) function that returns a typed module for testing, other than that I can't see how I could easily integrate this with the rest of the sdk.

  13. abettadapur commented on Jan 28, 2019

    @abettadapur
    CollaboratorAuthor

    abdurahmanus (@abdurahmanus) Good suggestion, I will create another section related to 'dependencies'

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