<commit_before>---
layout: post
title: "Decoding strings into TypeScript enums"
date: 2018-11-27 18:45:04
tags:
  - javascript
  - typescript
  - enum
  - typesafe
  - decode
---

Staying in a relatively safe world of TypeScript code, with strict null checks, is all fine and
dandy. Sometimes though one has to deal with the cruel outside world: JSON from the server or from
the localStorage, user input, route parameters, and so on. Then we have to shoehorn the uncontrolled
chaos from the outside into our nice algebraic type system which strives to making the impossible
states unrepresentable. Here's a simple example: an enumeration of Languages.

{% highlight typescript %}
enum Language {
  DE = "de",
  FR = "fr",
  IT = "it",
  EN = "en"
}
{% endhighlight %}

That's a handy thing to define, because, if you write a function returning some localized text, and
happen to forget about English…

{% highlight typescript %}
function thanks(language: Language): string {
  switch (language) {
    case Language.DE:
      return "Danke";

    case Language.FR:
      return "Merci";

    case Language.IT:
      return "Grazie";

    case Language.EN:
      return "Thanks";
  }
}
{% endhighlight %}

The TypeScript transpiler will complain:

    a.ts:8:40 - error TS2366: Function lacks ending return statement and return type does not include 'undefined'.

    8 function thanks(language: Language): string {
                                           ~~~~~~<commit_msg>Split line<commit_after>---
layout: post
title: "Decoding strings into TypeScript enums"
date: 2018-11-27 18:45:04
tags:
  - javascript
  - typescript
  - enum
  - typesafe
  - decode
---

Staying in a relatively safe world of TypeScript code, with strict null checks, is all fine and
dandy. Sometimes though one has to deal with the cruel outside world: JSON from the server or from
the localStorage, user input, route parameters, and so on. Then we have to shoehorn the uncontrolled
chaos from the outside into our nice algebraic type system which strives to making the impossible
states unrepresentable. Here's a simple example: an enumeration of Languages.

{% highlight typescript %}
enum Language {
  DE = "de",
  FR = "fr",
  IT = "it",
  EN = "en"
}
{% endhighlight %}

That's a handy thing to define, because, if you write a function returning some localized text, and
happen to forget about English…

{% highlight typescript %}
function thanks(language: Language): string {
  switch (language) {
    case Language.DE:
      return "Danke";

    case Language.FR:
      return "Merci";

    case Language.IT:
      return "Grazie";

    case Language.EN:
      return "Thanks";
  }
}
{% endhighlight %}

The TypeScript transpiler will complain:

    a.ts:8:40 - error TS2366: Function lacks ending return statement
    and return type does not include 'undefined'.

    8 function thanks(language: Language): string {
                                           ~~~~~~
{'fraction_non_alphanumeric': 0.08403930870891223, 'fraction_numerical': 0.014910199932226365, 'mean_word_length': 3.146067415730337, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3955945', 'n_tokens_mistral': 878, 'n_tokens_neox': 793, 'n_words': 361}