> For the complete documentation index, see [llms.txt](https://orasund.gitbook.io/elm-cookbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://orasund.gitbook.io/elm-cookbook/recipes-1/making-impossible-states-impossible/write-safer-functions-using-phantom-types.md).

# Write safer functions using Phantom Types

{% tabs %}
{% tab title="Problem" %}

```
type alias LoginForm =
    { username : String
    , password : String
    , isValid : Bool
    }

{-| Creates a User

Only use this function if the LoginForm was validated.
-}
createUser : LoginForm -> User
createUser loginForm =
    if loginForm.isValid then
        User.create loginForm.username
    else
        Debug.todo "This should not happen."
```

{% endtab %}

{% tab title="Solution" %}

```
type LoginForm valid =
    LoginForm
        { username : String
        , password : String
        }

type Valid = Valid

{-| Creates a User
-}
createUser : LoginForm Valid -> User
createUser (LoginForm loginForm) =
        User.create loginForm.username
        
validate : LoginForm () -> LoginForm Valid
validate =
    ...
```

{% endtab %}
{% endtabs %}

## Question

How can I  ensure that a user can only be created with a valid login form?

## Answer

Use a so called Phantom Type:

```
type LoginForm valid = --valid is not used in the definition
    LoginForm
        { username : String
        , password : String
        }
        
type Valid = Valid
```

Use `LoginForm ()` for unvalidated forms and `LoginForm Valid` for validated ones.

## Further reading

* 📄**Article:** [Advanced Types in Elm - Phantom Types](https://medium.com/@ckoster22/advanced-types-in-elm-phantom-types-808044c5946d) by Charlie Koster


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://orasund.gitbook.io/elm-cookbook/recipes-1/making-impossible-states-impossible/write-safer-functions-using-phantom-types.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
