# Correct way of using ports in 0.19

**URL:** <https://discourse.elm-lang.org/t/correct-way-of-using-ports-in-0-19/2327>\
**Category:** Request Feedback\
**Created:** [October 25, 2018, 9:08am UTC](https://discourse.elm-lang.org/t/correct-way-of-using-ports-in-0-19/2327 "2018-10-25T09:08:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![xarvh](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/xarvh/32/1025_2.png) [@xarvh](https://discourse.elm-lang.org/u/xarvh)\
**Post date:** [October 25, 2018, 9:08am UTC](https://discourse.elm-lang.org/t/correct-way-of-using-ports-in-0-19/2327/1 "2018-10-25T09:08:18Z")

</div>

In 0.19, the code

```auto
var elmApp = Elm.Main.init({ node: document.getElementById('app') });
elmApp.ports.somePortName.subscribe(whatever);

```

breaks if `somePortName` is declared but not _used_ in the Elm code (I assume that this is due to 0.19’s dead code removal feature).

This actually happened in one of my projects: the same JS module defined two different ports and both were declared Elm-side, but only one was actually used.

If I want my code to be reliable and prevent JS to break unexpectedly, I must add a check to every JS-side port declaration:

```auto
elmApp.ports.somePortName && elmApp.ports.somePortName.subscribe(whatever);

```

This doesn’t seem ideal to me, but seems necessary.  
Is this sensible? Am I missing something?

---

<div class="post-metadata">

**Author:** ![rupert](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rupert/32/1775_2.png) [@rupert](https://discourse.elm-lang.org/u/rupert)\
**Post date:** [October 25, 2018, 4:39pm UTC](https://discourse.elm-lang.org/t/correct-way-of-using-ports-in-0-19/2327/2 "2018-10-25T16:39:52Z")

</div>

Its worse, I just coded:

```
app.ports && app.ports.textToSVG &&
  app.ports.textToSVG.subscribe(request => {

```

To cater for the situation during dev where I have not yet wired up any ports, so `app.ports` is undefined…

---

<div class="post-metadata">

**Author:** ![Bandicoot](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/bandicoot/32/429_2.png) [@Bandicoot](https://discourse.elm-lang.org/u/Bandicoot)\
**Post date:** [November 1, 2018, 11:15pm UTC](https://discourse.elm-lang.org/t/correct-way-of-using-ports-in-0-19/2327/3 "2018-11-01T23:15:58Z")

</div>

That’s what I’m doing too.

---

<div class="post-metadata">

**Author:** ![Libbum](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/libbum/32/1311_2.png) [@Libbum](https://discourse.elm-lang.org/u/Libbum)\
**Post date:** [November 2, 2018, 3:55pm UTC](https://discourse.elm-lang.org/t/correct-way-of-using-ports-in-0-19/2327/4 "2018-11-02T15:55:13Z")

</div>

The second portion of “Types Without Borders” by Dillon Kearns from elm-conf 2018 tackles this issue using TypeScript. Looks to be a fairly robust solution IMO. Perhaps a bit more overhead initially, but far less pain later.

---

<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:** [November 12, 2018, 3:55pm UTC](https://discourse.elm-lang.org/t/correct-way-of-using-ports-in-0-19/2327/5 "2018-11-12T15:55:15Z")

</div>

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