title: Would an Option or Optional type (Option<T>) make sense in TypeScript?
body: <p>Languages like Haskell, Rust, among others, offer a <a href="https://hackage.haskell.org/package/base-4.9.1.0/docs/Prelude.html#t:Maybe" rel="noreferrer"><code>Maybe</code></a> or <a href="https://doc.rust-lang.org/std/option/enum.Option.html" rel="noreferrer"><code>Option</code></a> type. Even in Java, there is a <a href="https://docs.oracle.com/javase/8/docs/api/index.html?java/util/Optional.html" rel="noreferrer"><code>Optional</code></a> type nowadays.</p>
  
  <p>For simplicity, I will call this type an 'Option type' in the remaining question. '<em>Optional</em> type' is apparently often used to describe situations where providing type annotations is optional.</p>
  
  <p>I am curious about the following: does an Option type make sense in a language like TypeScript? The advantages of the Option type are quite convincing in other languages and I find myself missing the type when programming in TypeScript.</p>
  
  <p>Basically, the type system forces you to explicitly unwrap any value that may be inside an Option value. Yes, TypeScript's strict null-checking can also accomplish that, however, working with the Option type offers you an (in my opinion) much nicer way to handle potential <code>None</code> values by providing <code>map(f: T -&gt; U): Option&lt;U&gt;</code> and <code>mapOr(f: T -&gt; U, or: U): Option&lt;U&gt;</code> methods, etc.</p>
  
  <p>For instance, I would like something like the following code to work:</p>
  
  <pre><code>interface Foo {
      member: Option&lt;string&gt;
  }
  const opt: Option&lt;Foo&gt; = // ... some initialization
  const memberLength: number = opt
      .map(x =&gt; x.member) // None if x is None, else Some(...)
      .map(x =&gt; x.length) // None if x.member is None, else Some(x.length)
      .unwrapOrElse(() =&gt; 0);
  </code></pre>
  
  <p>This is, of course a very simple example, where using an Option type is a bit over-engineered. It should give a basic idea, though.</p>
  
  <p>I currently don't see a reason why this would be a bad idea, but no one seems to have implemented it, as far as I can tell. Would this have a serious performance impact? Or are there any other issues I can't see that make this non-viable?</p>
  
  <p><strong>Note:</strong> I am not (mainly) asking for how to implement this (although that is also an interesting topic -- but I have ideas for that). My main concern is finding out why no one seems to be using something like this yet.</p>