Skip to content

TypeScript augmentation challenge #9015

Closed

Description

@garthk

TypeScript Version: 1.8.0

Code Version: 1.2.0

Code:

I've put all the code in a gist, but the crux is that I want type system support for this line in example.ts:

store.dispatch(thunkedAction(1) as any);

I don't want to have to cast the thunkedAction result to any, but it's the only way I've found to make the compiler happy. It might be any of the following, but I've no idea which:

  • Compiler bug
  • Compiler feature gap edge case due to combination of typings globals, typings modules, npm modules, and local .d.ts files
  • Insufficiently helpful documentation
  • Insufficiently helpful messages
  • A developer so frustrated he can no longer think straight

I sincerely hope it's the latter, as then someone will be able to set me straight quickly.

Typings supporting the above code:

declare module "redux-thunk" {
    import { Middleware, Dispatch } from 'redux';

    export interface Thunk extends Middleware {}
    export interface ThunkInterface<R> {
      <S>(dispatch: Dispatch<S>, getState?: () => S): R;
    }

    var thunk: Thunk;
    export default thunk;
}

Failing augmentation for redux.Dispatch which seems to override node_modules/redux/index.d.ts for the entire project:

declare module "redux" {
    export interface Dispatch<S> {
        <R>(thunkAction: ThunkInterface<R>): R;
    }
}

Expected behavior:

Per reduxjs/redux#1712, being able to augment redux.Dispatch<S>.

Actual behavior:

Wailing, gnashing of teeth, dumpster fires, sirens in the distance.

Activity

  1. mhegazy commented on Jun 8, 2016

    @mhegazy
    Contributor

    So i am not familiar with redux to know if the issue is in the usage or the declaration. The module augmentation seems to work fine for me using your project i am not sure i understand what is the issue.

    animation

    A note for future issue reporting, a git repo is much better way to share the state than a gist.

  2. domgee commented on Jun 8, 2016

    @domgee

    Mohamed Hegazy (@mhegazy) The problem is that if you put the module augmentation in a separate file then it hides the module definition in node_modules/redux.

    Garth Kidd (@garthk) The only way I have found to get it working is to move redux's index.d.ts out of node_modules and wrap it's contents with

    declare module 'redux' { ... }

    Then module augmentations work as expected. I don't know enough about TypeScript to know why this behaves differently to using the typings distributed with redux, or how to solve it in a better way. What I have is an npm postinstall script that does all this, and as unsatisfactory as it is, it is the only way I have found that works.

    If you or anyone else finds a better way, please let me know!

  3. mhegazy commented on Jun 8, 2016

    @mhegazy
    Contributor

    Mohamed Hegazy (@mhegazy) The problem is that if you put the module augmentation in a separate file then it hides the module definition in node_modules/redux.

    The file needs to be a module. just like i did.

    so something like this should work:

    /// myAugmentationFile.ts
    import * as redux from "redux";
    
    declare module "redux" {
        export interface Dispatch<S> {
            <R>(thunkAction: ThunkInterface<R>): R;
        }
    }
  4. domgee commented on Jun 8, 2016

    @domgee

    Yes, that works! I was just missing the import statement at the top of my augmentation file.

    Thank you Mohamed Hegazy (@mhegazy)!

  5. added
    QuestionAn issue which isn't directly actionable in code
    and removed
    Needs More InfoThe issue still hasn't been fully clarified
    on Jun 8, 2016
  6. mhegazy commented on Jun 8, 2016

    @mhegazy
    Contributor

    glad it is resolved.

  7. locked and limited conversation to collaborators on Jun 19, 2018
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

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions