Skip to content

TypeScript augmentation challenge #9015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
garthk opened this issue Jun 8, 2016 · 5 comments
Closed

TypeScript augmentation challenge #9015

garthk opened this issue Jun 8, 2016 · 5 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@garthk
Copy link

garthk commented Jun 8, 2016

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.

@mhegazy
Copy link
Contributor

mhegazy commented Jun 8, 2016

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.

@mhegazy mhegazy added the Needs More Info The issue still hasn't been fully clarified label Jun 8, 2016
@domgee
Copy link

domgee commented Jun 8, 2016

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

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

@mhegazy
Copy link
Contributor

mhegazy commented Jun 8, 2016

@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;
    }
}

@domgee
Copy link

domgee commented Jun 8, 2016

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

Thank you @mhegazy!

@mhegazy mhegazy added Question An issue which isn't directly actionable in code and removed Needs More Info The issue still hasn't been fully clarified labels Jun 8, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Jun 8, 2016

glad it is resolved.

@mhegazy mhegazy closed this as completed Jun 8, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants