Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Would an Option or Optional type (Option<T>) make sense in TypeScript?

Languages like Haskell, Rust, among others, offer a Maybe or Option type. Even in Java, there is a Optional type nowadays.

For simplicity, I will call this type an 'Option type' in the remaining question. 'Optional type' is apparently often used to describe situations where providing type annotations is optional.

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.

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 None values by providing map(f: T -> U): Option<U> and mapOr(f: T -> U, or: U): Option<U> methods, etc.

For instance, 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 is, of course a very simple example, where using an Option type is a bit over-engineered. It should give a basic idea, though.

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?

Note: 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.

like image 250
anty Avatar asked May 30 '17 16:05

anty


People also ask

Does TypeScript have an option type?

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 None values by providing map(f: T -> U): Option<U> and mapOr(f: T -> U, or: U): Option<U> methods, etc.

What is optional type in TypeScript?

You must tell TypeScript if a property is optional. First, if you don't tell TypeScript that a property is optional, it will expect it to be set. TypeScript. type Foo = { bar: number; } const a: Foo = {}; // This is an error: // Property 'bar' is missing in type '{}' but required in type 'Foo'.

Should I use type or interface TypeScript?

Interfaces are most recommended for defining new objects or methods or properties of an object where it will receive a specific component. Hence interface works better when using objects and method objects. Therefore it is our choice to choose between types or interface according to the program needs.

Should you type everything in TypeScript?

Yes, you should make it a habit to explicitly set all types, it's will prevent having unexpected values and also good for readability.

Does the option type make sense in a language like typescript?

' Optional type' is apparently often used to describe situations where providing type annotations is optional. 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.

How do you specify an optional property in typescript?

When defining a type in TypeScript, we can specify that a property is optional with a question mark after the name: Or we can specify that a property may be undefined: These two interfaces seem nearly identical.

How to use generic parameter defaults in typescript?

As of TypeScript 2.3, you can use generic parameter defaults. private logData<T, S = {}> (operation: string, responseData: T, requestData?: S) { // your implementation here } Curious if using S = never would enforce the type get specified when the optional parameter is used. TS Update 2020: Giving void will make the generic type optional.

Why do I get function arguments errors in typescript?

In TypeScript, the compiler checks every function call and issues an error in the following cases: The number of arguments is different from the number of parameters specified in the function. Or the types of arguments are not compatible with the types of function parameters.


1 Answers

EDIT 2019

Check fp-ts

Original Answer

I was missing it coming from Scala (and some Haskell) so I made my own lib for it (and a couple others): MM (MIT license)

Feel free to hack, fork etc...; it is MIT licensed

like image 81
Bruno Grieder Avatar answered Oct 07 '22 00:10

Bruno Grieder