我刚刚开始学习Elm而且我遇到了类型注释问题。
此代码有效:
viewInput : String -> Html msg
viewInput myText =
div [ style [("color","red")] ] [ text myText ]
这会引发编译器异常:
viewInput : String -> Html msg
viewInput myText =
input [ type' "text", placeholder myText ]
错误是
-- TYPE MISMATCH ------------------------------------------------------ form.elm
The type annotation for `viewInput` does not match its definition.
62| viewInput : String -> Html msg
^^^^^^^^^^^^^^^^^^
The type annotation is saying:
String -> Html a
But I am inferring that the definition has this type:
String -> List (Html a) -> Html a
Detected errors in 1 module.
答案 0 :(得分:2)
我认为你最后只是遗漏了一些括号......
您的代码应为
viewInput : String -> Html msg
viewInput myText =
input [ type' "text", placeholder myText ] []
这是因为input
函数,div
确实需要输入两个列表,一个用于属性,一个用于包含的其他Html
件