From eed5135e5ffdec1d15136af1f2310bdd453d77e4 Mon Sep 17 00:00:00 2001 From: gcanti Date: Mon, 11 Nov 2019 09:00:45 +0100 Subject: [PATCH] `fromNullable` now uses `NonNullable` in its return type, fixes #1004 --- CHANGELOG.md | 5 +++++ docs/modules/Either.ts.md | 2 +- docs/modules/Option.ts.md | 2 +- dtslint/ts3.5/index.ts | 25 +++++++++++++++++++++++++ package.json | 2 +- src/Either.ts | 4 ++-- src/Option.ts | 4 ++-- 7 files changed, 37 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b5433c684..2c3f9b719 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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** diff --git a/docs/modules/Either.ts.md b/docs/modules/Either.ts.md index af1b9512e..e85800f15 100644 --- a/docs/modules/Either.ts.md +++ b/docs/modules/Either.ts.md @@ -246,7 +246,7 @@ the provided default as a `Left` **Signature** ```ts -export function fromNullable(e: E): (a: A | null | undefined) => Either { ... } +export function fromNullable(e: E): (a: A) => Either> { ... } ``` **Example** diff --git a/docs/modules/Option.ts.md b/docs/modules/Option.ts.md index 8b8665359..0a788d398 100644 --- a/docs/modules/Option.ts.md +++ b/docs/modules/Option.ts.md @@ -275,7 +275,7 @@ returns the value wrapped in a `Some` **Signature** ```ts -export function fromNullable(a: A | null | undefined): Option { ... } +export function fromNullable(a: A): Option> { ... } ``` **Example** diff --git a/dtslint/ts3.5/index.ts b/dtslint/ts3.5/index.ts index 27dd3dd54..2f15288d3 100644 --- a/dtslint/ts3.5/index.ts +++ b/dtslint/ts3.5/index.ts @@ -167,6 +167,31 @@ type C = A | B // $ExpectError O.getRefinement(c => (c.type === 'B' ? O.some(c) : O.none)) +// fromNullable + +declare const fromNullableTest1: number | null | undefined +O.fromNullable(fromNullableTest1) // $ExpectType Option + +interface fromNullableTest2 { + foo: number | undefined +} +declare const fromNullableTest3: (key: Key) => fromNullableTest2[Key] +// $ExpectType Option +Fu.flow( + fromNullableTest3, + O.fromNullable +)('foo') + +// +// Either +// + +// $ExpectType Either +Fu.flow( + fromNullableTest3, + E.fromNullable('error') +)('foo') + // // HKT // diff --git a/package.json b/package.json index 1de80d394..68e8fab47 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fp-ts", - "version": "2.1.1", + "version": "2.1.2", "description": "Functional programming in TypeScript", "files": [ "lib", diff --git a/src/Either.ts b/src/Either.ts index b5bf62a02..869147aff 100644 --- a/src/Either.ts +++ b/src/Either.ts @@ -118,8 +118,8 @@ export function right(a: A): Either { * * @since 2.0.0 */ -export function fromNullable(e: E): (a: A | null | undefined) => Either { - return a => (a == null ? left(e) : right(a)) +export function fromNullable(e: E): (a: A) => Either> { + return (a: A) => (a == null ? left(e) : right(a as NonNullable)) } /** diff --git a/src/Option.ts b/src/Option.ts index e3a710aec..de8791194 100644 --- a/src/Option.ts +++ b/src/Option.ts @@ -160,8 +160,8 @@ export function fold(onNone: () => B, onSome: (a: A) => B): (ma: Option * * @since 2.0.0 */ -export function fromNullable(a: A | null | undefined): Option { - return a == null ? none : some(a) +export function fromNullable(a: A): Option> { + return a == null ? none : some(a as NonNullable) } /**