Skip to content

Commit

Permalink
#248 Add union overload to ThunkDispatch (#255)
Browse files Browse the repository at this point in the history
* #248 Add union overload to ThunkDispatch

See discussion in #248 and microsoft/TypeScript#14107. Without this explicit overload, TypeScript is unable to figure out that the function can be called with an argument of type `T|ThunkAction<...>`.

* Merge ThunkDispatch union overload with renamed type parameters

Co-Authored-By: Tim Dorr <[email protected]>
  • Loading branch information
Philipp91 and timdorr committed Jul 6, 2019
1 parent ffd8834 commit 237f6bb
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ export interface ThunkDispatch<
thunkAction: ThunkAction<TReturnType, TState, TExtraThunkArg, TBasicAction>
): TReturnType;
<A extends TBasicAction>(action: A): A;
// This overload is the union of the two above (see TS issue #14107).
<TReturnType, TAction extends TBasicAction>(
action:
| TAction
| ThunkAction<TReturnType, TState, TExtraThunkArg, TBasicAction>
): TAction | TReturnType;
}

/**
Expand Down

1 comment on commit 237f6bb

@Yaojian
Copy link

@Yaojian Yaojian commented on 237f6bb Jul 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The declaration still now work:

    export function AssertIs<T, U extends T>() {}

    it('should resolve to the thunk overload when dispatching thunk actions', () => {
        const dispatch = (() => undefined) as ThunkDispatch<any, any, any>;
        const action = {} as ThunkAction<Promise<number>, any, undefined, Action>;
        const v = dispatch(action);

        // the following statement cannot be compiled, which indicates the wrong overload is picked
        AssertIs<Promise<number>, typeof v>();
    });

I think it's better to define ThunkDispatch as:

    export interface ThunkDispatch<S, E, A extends Action> {
        <A>(action: A): A extends (...args: any) => infer R ? R : A;
    }

Please sign in to comment.