Last active
April 22, 2020 00:39
-
-
Save tvler/ccbb3136b0d8c6c48ca53d98382dcb05 to your computer and use it in GitHub Desktop.
Implicitly type an action payload by an action type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type ActionTypes = 'actionA' | 'actionB' | 'actionC' | 'actionNoPayload'; | |
type ActionPayloads = { | |
actionA: { payload: number }; | |
actionB: { payload: string }; | |
actionC: { foo: boolean; bar: boolean }; | |
}; | |
type Actions = { | |
[key in ActionTypes]: { | |
type: key; | |
} & (key extends keyof ActionPayloads ? ActionPayloads[key] : {}) | |
}; | |
type Action = Actions[keyof Actions]; | |
// Pass | |
const testA: Action = { type: 'actionA', payload: 1 }; | |
const testB: Action = { type: 'actionB', payload: 'string' }; | |
const testC: Action = { type: 'actionC', foo: true, bar: false }; | |
const testD: Action = { type: 'actionNoPayload' }; | |
// Fail | |
const testUnexpectedActionTypeError: Action = { type: 'not typed' }; | |
const testUnexpectedPayloadError: Action = { type: 'actionNoPayload', payload: 'error' }; | |
const testExpectedPayloadError: Action = { type: 'actionA' }; | |
const testUnexpectedPayloadValueError: Action = { type: 'actionA', payload: 'error' }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment