Skip to content

strongly typed reducer action type #1

@OluwatobilobaIbuola

Description

@OluwatobilobaIbuola

I supposed that the reducer action type from lesson 16 and lesson 17 should be typed based on the object key type instead of a string. This will foster code consistency as the type manipulated (Union type) from the key of the object depends strongly on the key of the object provided that the object is asserted with const. And that only the literal types from the union can only be passed to the action type instead of any strings.

Example of a successful runtime error that which static type checker could not help (This can occur when working with someone else that felt like creating another action but misspelled the action-type without a reference to the object created by the author):

const onSubmitOrder = () => {
    dispatch({ type: "SUBMI" })
    setConfirm(true)
}

The below image shows the implementation by asserting const and then creating a type from the key of the object
I also implemented this using enum in one of the lessons (lessons 16 | lessons 17)

dave-gray-ts

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions