Skip to content

Instantly share code, notes, and snippets.

@punmechanic
Last active July 15, 2019 23:11
Show Gist options
  • Save punmechanic/703f0df28ce6035e174d940237891666 to your computer and use it in GitHub Desktop.
Save punmechanic/703f0df28ce6035e174d940237891666 to your computer and use it in GitHub Desktop.
type Action =
| { type: 'ReturnsPromise' }
| { type: 'ReturnsUndefined' }
type R<A> =
| A extends { type: 'ReturnsPromise' } ? Promise<unknown> : never
| A extends { type: 'ReturnsUndefined' } ? undefined : never
| unknown
type Dispatch<A> = (action: A) => R<A>
const dispatch = <A>(action: A) => undefined as R<A>
dispatch({ type: 'ReturnsPromise' as const }).then(() => foo())
// failure: dispatch returns undefined (expected)
dispatch({ type: 'ReturnsUndefined' as const }).then(() => null)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment