Search code examples
purescriptpurescript-pux

Error could not match type


I have a type:

newtype User = User
  { id :: String
  , email :: String
  , last_update :: String
  }

and a function:

import Pux.DOM.HTML (HTML)
import Pux.DOM.HTML.Attributes (key)
import Text.Smolder.HTML as H
import Text.Smolder.HTML.Attributes as A

userRow :: User -> HTML Event
  userRow user =
    H.tr ! key user.id ! A.className "user-item" $ do
      H.td ! key ("email") $ text user.email
      H.td ! key ("last_update") $ text user.last_update
      H.td ! key ("actions") ! A.className "actions" $ do
        H.a ! key ("delete")  ! A.className "action-delete" #! onClick (pure $ OpenDeleteModal (show user.id)) $ do
          H.span
          ! A.className "dashicons dashicons-trash"
          ! A.alt "Delete"
          ! A.title "Delete"
          $ text ""

But the types will not unify:

268    H.tr ! key user.id ! A.className "user-item" $ do
                  ^^^^

Could not match type

  { id :: String
  | t0
  }

with type

  User

while checking that type User
  is at least as general as type { id :: String
                                 | t0
                                 }
while checking that expression user
  has type { id :: String
           | t0
           }
while checking type of property accessor user.id
in value declaration userRow

where t0 is an unknown type

I can't see what I am doing wrong. It seems to me that the user type has an "id" member which is a string, and therefore is at least as general as

type { id :: String
     | t0
     }

Solution

  • User is a newtype around a record, so it wraps the record up. In order to access the records fields you first need to unwrap it. The easiest way to do this in your situation is to pattern match on the user argument:

    userRow :: User -> HTML Event
    userRow (User user) =
      H.tr ! key user.id ! A.className "user-item" $ do
        ...