
Company News
/Security News
Socket Selected for OpenAI's Cybersecurity Grant Program
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.
Correct, easy to use Option type for TypeScript. Like Scala options; see the introductory blog post.
# Using Yarn:
yarn add tsoption
# Or, using NPM:
npm install tsoption --save
Note: You can use JavaScript instead of TypeScript, but it's not as fun.
let a = Option.of(3) // Some<number>(3)
.flatMap(_ => Some.of(5)) // Some<number>(5)
.map(_ => 'a') // Some<string>('a')
.orElse(Option.of('b')) // Some<string>('a') (non-string type gives a compile error)
.getOrElse('c') // 'a'
let b = Option.of('a') // Some<string>('a')
.flatMap(_ => new None) // None<string>() (flatMap can map to any type)
.orElse(Option.of('b')) // Some<string>('b') (non-string type gives a compile error)
.get() // 'b'
Note: The types of each of the expressions below are known at compile time.
// Create an option
Option.of(3) // Some(3)
Option.of('abc') // Some('abc')
Option.of(null) // None (for convenience)
Option.of(undefined) // None (for convenience)
Some.of(3) // Some(3)
Some.of(null) // Some(null)
new Some([1, 2, 3]) // Some([1, 2, 3])
new None // None
// #flatMap
Some.of(3).flatMap(_ => Some.of(_ * 2)) // Some(6)
None.of().flatMap(() => Some.of(2)) // None
// #get
Some.of(3).get() // 3
None.of().get() // COMPILE ERROR! Can't call get() on None
// #getOrElse
Some.of(1).getOrElse(2) // 1
None.of().getOrElse(2) // 2
// #isEmpty
Some.of(2).isEmpty() // false (known at compile time too!)
None.of().isEmpty() // true (known at compile time too!)
// #map
Some.of(2).map(_ => _ * 2) // Some(4)
None.of().map(() => 2) // None (known at compile time too!)
// #nonEmpty
Some.of(2).nonEmpty() // true (known at compile time too!)
None.of().nonEmpty() // false (known at compile time too!)
// #orElse
Some.of(2).orElse(Option.of(3)) // Some(2)
None.of().orElse(Option.of(3)) // Some(3)
// #toString
Some.of(2).toString() // "Some(2)"
None.of().toString() // "None"
TSOption is Fantasyland-compliant. It implements:
import * as fl from 'fantasy-land'
const {ap, chain, map, of} = fl
// Create an option
Option[of](3) // Some(3)
Option[of]('abc') // Some('abc')
Option[of](null) // None
Option[of](undefined) // None
// #chain
Option[of](3)[chain](_ => Option[of](_ * 2)) // Some(6)
Option[of](null)[chain](() => Option[of](2)) // None (known at compile time too!)
// #map
Option[of](2)[map](_ => _ * 2) // Some(4)
Option[of](null)[map](() => 2) // None (known at compile time too!)
// #ap
Option[of](2)[ap](Option[of]((_: number) => _ * 2)) // Some(4)
Option[of](null)[ap](Option[of](() => 2)) // None (known at compile time too!)
npm test
MIT
FAQs
Correct, easy to use Option type for TypeScript
We found that tsoption demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
/Security News
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.

Security News
Socket CEO Feross Aboukhadijeh joins 10 Minutes or Less, a podcast by Ali Rohde, to discuss the recent surge in open source supply chain attacks.

Research
/Security News
Campaign of 108 extensions harvests identities, steals sessions, and adds backdoors to browsers, all tied to the same C2 infrastructure.