Skip to content

Commit

Permalink
fromNullable now uses NonNullable in its return type, fixes #1004
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Nov 12, 2019
1 parent 5ba4716 commit eed5135
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
**Note**: Gaps between patch versions are faulty/broken releases. **Note**: A feature tagged as Experimental is in a
high state of flux, you're at risk of it changing without notice.

# 2.1.2

- **Bug Fix**
- `fromNullable` now uses `NonNullable` in its return type, fixes #1004 (@gcanti)

# 2.1.1

- **Bug Fix**
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/Either.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ the provided default as a `Left`
**Signature**

```ts
export function fromNullable<E>(e: E): <A>(a: A | null | undefined) => Either<E, A> { ... }
export function fromNullable<E>(e: E): <A>(a: A) => Either<E, NonNullable<A>> { ... }
```

**Example**
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/Option.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ returns the value wrapped in a `Some`
**Signature**

```ts
export function fromNullable<A>(a: A | null | undefined): Option<A> { ... }
export function fromNullable<A>(a: A): Option<NonNullable<A>> { ... }
```

**Example**
Expand Down
25 changes: 25 additions & 0 deletions dtslint/ts3.5/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,31 @@ type C = A | B
// $ExpectError
O.getRefinement<C, A>(c => (c.type === 'B' ? O.some(c) : O.none))

// fromNullable

declare const fromNullableTest1: number | null | undefined
O.fromNullable(fromNullableTest1) // $ExpectType Option<number>

interface fromNullableTest2 {
foo: number | undefined
}
declare const fromNullableTest3: <Key extends keyof fromNullableTest2>(key: Key) => fromNullableTest2[Key]
// $ExpectType Option<number>
Fu.flow(
fromNullableTest3,
O.fromNullable
)('foo')

//
// Either
//

// $ExpectType Either<string, number>
Fu.flow(
fromNullableTest3,
E.fromNullable('error')
)('foo')

//
// HKT
//
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fp-ts",
"version": "2.1.1",
"version": "2.1.2",
"description": "Functional programming in TypeScript",
"files": [
"lib",
Expand Down
4 changes: 2 additions & 2 deletions src/Either.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ export function right<E = never, A = never>(a: A): Either<E, A> {
*
* @since 2.0.0
*/
export function fromNullable<E>(e: E): <A>(a: A | null | undefined) => Either<E, A> {
return a => (a == null ? left(e) : right(a))
export function fromNullable<E>(e: E): <A>(a: A) => Either<E, NonNullable<A>> {
return <A>(a: A) => (a == null ? left(e) : right(a as NonNullable<A>))
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ export function fold<A, B>(onNone: () => B, onSome: (a: A) => B): (ma: Option<A>
*
* @since 2.0.0
*/
export function fromNullable<A>(a: A | null | undefined): Option<A> {
return a == null ? none : some(a)
export function fromNullable<A>(a: A): Option<NonNullable<A>> {
return a == null ? none : some(a as NonNullable<A>)
}

/**
Expand Down

0 comments on commit eed5135

Please sign in to comment.