Skip to content

Commit 461a9a0

Browse files
committed
add to README.
1 parent f6b0fcb commit 461a9a0

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

README.md

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ const optionalEmpty1: Optional<string> = Optional.empty(); // type hinting requi
4949
const optionalEmpty2 = Optional.empty<string>(); // or parameterize explicitly
5050
```
5151

52-
53-
5452
### operations
5553

5654
```ts
@@ -122,28 +120,28 @@ optional.toOption();
122120
### prototype-free types
123121

124122
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!).
123+
relying on `prototype` can cause some problems in certain situations like that an external function copies such objects *except* `prototype`.
124+
For example, `setState` of React reflects the given value as a state except the value's `prototype` (and then you will see "TypeError: undefined is not a function" in runtime though TypeScript compilation has been succeeded!).
127125

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.).
126+
To avoid this issue, you have three options that *convert* an `Optional` into a *prototype-free*, or a simple JavaScript object (associative array, string etc.).
129127

130-
- `Option.orNull`
131-
- `Option.orUndefined`
132-
- `Option.toOption`
128+
- `Optional.orNull`
129+
- `Optional.orUndefined`
130+
- `Optional.toOption`
133131

134-
#### `Option.orNull` and `Option.orUndefined`
132+
#### `Optional.orNull` and `Optional.orUndefined`
135133

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*.
134+
Using `Optional.orNull` or `Optional.orUndefined` is the simple way to obtain prototype-free objects.
135+
These methods convert an `Optional<T>` into a value of *type union*.
138136

139-
`Option<T>.orNull` returns `T | null`.
137+
`Optional<T>.orNull` returns `T | null`.
140138

141139
`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.
142140

143141
Use `Optional.ofNullable` to restore an Optional value from a value of these type unions.
144142

145143
```ts
146-
const update: <T> (original: T) => T = /* some external function */
144+
const update: <T> (original: T) => T = /* some external function that returns without the prototype */
147145
const optional: Optional<string> = /* some Optional object */;
148146

149147
let nullable: string | null = optional.orNull();
@@ -162,9 +160,20 @@ const optionalFromOrUndefined: Optional<string> = Optional.ofNullble(orUndefined
162160

163161
#### `Option.toOption`
164162

163+
As a more explicit way to obtain prototype-free objects, `Optional.toOption` is provided.
164+
This method convert an `Optional<T>` into an object of `Option<T>` type, which conforms to [*discriminated unions*](http://www.typescriptlang.org/docs/handbook/advanced-types.html#discriminated-unions) also known as *algebraic data types*.
165+
Refer the [API document](lib/types.ts) of `Option<T>` to learn about the structure.
166+
165167
```ts
168+
const update: <T> (original: Option<T>) => T = /* some external function that returns without the prototype */
166169
const optional: Optional<string> = /* some Optional value */;
167-
const option: Option<string> = optional.toOption();
170+
171+
let option: Option<string> = optional.toOption();
172+
173+
// update using external functions!
174+
option = update(option);
175+
176+
// restore from Option<T>.
168177
const optionalFromOption: Optional<string> = Optional.from(option);
169178
```
170179

0 commit comments

Comments
 (0)