Will Option or Option (Option <T>) make sense in TypeScript?
Languages ​​such as Haskell, Rust, among others, offer Maybe or Option . Even in Java, Optional now exists.
For simplicity, I will call this type “Option type” in the remaining question. An “optional type” appears to be often used to describe situations where providing type annotations is optional.
I am interested in learning the following: Does the Option type make sense in a TypeScript language? The benefits of the Option type are pretty convincing in other languages, and I found that there is no type when programming in TypeScript.
Basically, the type system forces you to explicitly expand any value that may be inside the parameter value. Yes, TypeScript strict checking of zeros can also be performed, however, working with the Option type offers you (in my opinion) a much better way to handle potential None values ​​by providing the map(f: T -> U): Option<U> methods map(f: T -> U): Option<U> and mapOr(f: T -> U, or: U): Option<U> , etc.
For example, I would like something like the following code to work:
interface Foo { member: Option<string> } const opt: Option<Foo> = // ... some initialization const memberLength: number = opt .map(x => x.member) // None if x is None, else Some(...) .map(x => x.length) // None if x.member is None, else Some(x.length) .unwrapOrElse(() => 0); This, of course, is a very simple example where the use of the Option type is slightly redesigned. However, this should give a basic idea.
Currently, I see no reason why this would be a bad idea, but no one seems to have implemented it, as far as I can tell. Will this have a major impact on performance? Or are there any other problems that I don’t see to make this unviable?
Note: I do not (basically) ask how to implement this (although this is also an interesting topic, but I have ideas for this). My main concern is to find out why no one seems to be using something like this.