You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// retrieve a payload if this is present, return the given value otherwise.
92
+
// if this is present retrieve the payload,
93
+
// otherwise return the given value.
90
94
optional.orElse("bar");
91
95
92
-
// retrieve a payload if this is present, return a value supplied by the given function otherwise.
96
+
// if a payload is present, retrieve the payload,
97
+
// otherwise return a value supplied by the given function.
93
98
optional.orElseGet(() =>"bar");
94
99
95
-
// retrieve a payload if this is present, throws an exception supplied by the given function otherwise.
100
+
// if a payload is present, retrieve the payload,
101
+
// otherwise throw an exception supplied by the given function.
96
102
optional.orElseThrow(() =>newError());
103
+
104
+
// if a payload is present, retrieve the payload,
105
+
// otherwise return null.
106
+
optional.orNull();
107
+
108
+
// if a payload is present, retrieve the payload,
109
+
// otherwise return undefined.
110
+
optional.orUndefined();
111
+
112
+
// return an appropriate result by emulating pattern matching with the given cases.
113
+
optional.matches({
114
+
present: value=>value.length,
115
+
empty: () =>0,
116
+
})
117
+
118
+
// convert this to an Option value.
119
+
optional.toOption();
120
+
```
121
+
122
+
### prototype-free types
123
+
124
+
While `Optional`'s fluent interface for method chaining with `prototype` is usually useful and elegant,
125
+
relying on `prototype` can cause some problems in certain situations like an external function that copies such objects *except*`prototype`.
126
+
For example, `setState` of React reflects the given value as a state except its `prototype` (and then you will see "TypeError: undefined is not a function" in runtime though TypeScript compilation has been succeeded!).
127
+
128
+
To avoid this issue, you have three options that *open* an `Optional` into a *prototype-free*, or a simple JavaScript object (associative array, string etc.).
129
+
130
+
-`Option.orNull`
131
+
-`Option.orUndefined`
132
+
-`Option.toOption`
133
+
134
+
#### `Option.orNull` and `Option.orUndefined`
135
+
136
+
Using `Option.orNull` or `Option.orUndefined` is the simple way to obtain prototype-free objects.
137
+
These methods convert an Optional<T> into a value of *type union*.
138
+
139
+
`Option<T>.orNull` returns `T | null`.
140
+
141
+
`Optional<T>.orUndefined` returns `T | undefined`. The `T | undefined` type is compatible with [optional parameters and properties](http://www.typescriptlang.org/docs/handbook/advanced-types.html#optional-parameters-and-properties) of TypeScript.
142
+
143
+
Use `Optional.ofNullable` to restore an Optional value from a value of these type unions.
144
+
145
+
```ts
146
+
const update: <T> (original:T) =>T=/* some external function */
147
+
const optional:Optional<string> =/* some Optional object */;
148
+
149
+
let nullable:string|null=optional.orNull();
150
+
let orUndefined:string|undefined=optional.orUndefined();
0 commit comments