# Single type annotation

**URL:** <https://discourse.elm-lang.org/t/single-type-annotation/1088>\
**Category:** Learn\
**Created:** [April 7, 2018, 1:47pm UTC](https://discourse.elm-lang.org/t/single-type-annotation/1088 "2018-04-07T13:47:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ShalokShalom](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/shalokshalom/32/2203_2.png) [@ShalokShalom](https://discourse.elm-lang.org/u/ShalokShalom)\
**Post date:** [April 7, 2018, 1:47pm UTC](https://discourse.elm-lang.org/t/single-type-annotation/1088/1 "2018-04-07T13:47:20Z")

</div>

Hi there 🙂

I am a newbie, how you can infer from this question:

![](https://cdck-file-uploads-canada1.s3.dualstack.ca-central-1.amazonaws.com/flex035/uploads/elm_lang/original/1X/1adf2d0ad8b90add263083cd4050314608f6e50d.png)

`RoomID : String` is how it works in my imagination.

Why does it work otherwise? 😄

---

<div class="post-metadata">

**Author:** ![DanAbrams](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/danabrams/32/757_2.png) [@DanAbrams](https://discourse.elm-lang.org/u/DanAbrams)\
**Post date:** [April 7, 2018, 1:57pm UTC](https://discourse.elm-lang.org/t/single-type-annotation/1088/2 "2018-04-07T13:57:47Z")

</div>

Are you saying that you want RoomId to be an alias for String?

That would look something like this:

`type alias RoomId = String`

But if RoomId is just a string, you could also declare the record like so:

````auto
    { roomId: String
    , details...etc ```
````

---

<div class="post-metadata">

**Author:** ![Brian\_Carroll](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/brian_carroll/32/444_2.png) [@Brian\_Carroll](https://discourse.elm-lang.org/u/Brian_Carroll)\
**Post date:** [April 7, 2018, 3:42pm UTC](https://discourse.elm-lang.org/t/single-type-annotation/1088/3 "2018-04-07T15:42:38Z")

</div>

When there are syntax errors in the code, it really helps to spell out what you’re trying to do, because it’s hard for other people to understand the intention behind the code otherwise. Can you confirm what you are trying to do here?

My guess is you are trying to wrap a string in a custom type so that you can’t pass any old string instead of a room ID? The rest of this answer assumes that.

The correct syntax is:

```
type RoomId = RoomId String

```

The first `RoomId` is the name of a _type_, and the second `RoomId` is a _type constructor_.

You could also write:

```
type RoomId = RoomIdConstructor String

```

In Elm, types and type constructors exist in different namespaces, so you can use the same name for both and the compiler never gets them mixed up. People often do this. It saves you thinking of another name, and it’s so common in Elm code that you get used to it quickly. Confusing for beginners though!

You can also use the type constructor as a function. You apply it to a `String` and it gives you back a `RoomId`.

So why didn’t your version work?

```
RoomID : String

```

The `:` syntax applies a _type_ to a _value_. So the compiler expects a value on the left and a type on the right. But you are giving it a type on both sides, so that’s an error.  
If you actually wanted to say “all values of type RoomID are also type String”, then this would mean `RoomID` is just an alias for `String`, and you would use the `type alias` syntax, as @DanAbrams mentioned.  
Hope this helps, feel free to ask further questions otherwise! 🙂

---

<div class="post-metadata">

**Author:** ![ShalokShalom](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/shalokshalom/32/2203_2.png) [@ShalokShalom](https://discourse.elm-lang.org/u/ShalokShalom)\
**Post date:** [April 7, 2018, 5:54pm UTC](https://discourse.elm-lang.org/t/single-type-annotation/1088/4 "2018-04-07T17:54:55Z")

</div>

> [@DanAbrams](#):
>
> Are you saying that you want RoomId to be an alias for String?

Well, I wanted to declare the type RoomID as a String, if that makes sense.  
As it turns out, there was an underlying issue:

A union type is a correct technique here, so:

```auto
type RoomId 
    = RoomId String
```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [May 7, 2018, 5:56pm UTC](https://discourse.elm-lang.org/t/single-type-annotation/1088/5 "2018-05-07T17:56:41Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
