Skip to content

Commit b702da8

Browse files
fix: result.maybeOk() should constraint T to NonNullable<T> (#156)
1 parent 038ab69 commit b702da8

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

src/result/result.interface.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface IResultMatchPattern<T, E, U> {
1010
export interface IResult<T, E> {
1111
isOk(): boolean
1212
isFail(): boolean
13-
maybeOk(): IMaybe<T>
13+
maybeOk(): IMaybe<NonNullable<T>>
1414
maybeFail(): IMaybe<E>
1515
unwrap(): T | never
1616
unwrapOr(opt: T): T

src/result/result.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ describe('result', () => {
2727
})
2828

2929
it('should ...', () => {
30-
const _sut = ok('Test')
30+
const _sut = ok<string | undefined, string>('Test')
3131
.maybeOk()
32+
.map(b => b)
3233
.valueOr('Some Other')
3334

3435
expect(_sut).toEqual('Test')

src/result/result.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export abstract class Result<TOk, TFail> implements IResult<TOk, TFail> {
1717
abstract unwrap(): TOk | never
1818
abstract unwrapOr(opt: TOk): TOk
1919
abstract unwrapFail(): TFail | never
20-
abstract maybeOk(): IMaybe<TOk>
20+
abstract maybeOk(): IMaybe<NonNullable<TOk>>
2121
abstract maybeFail(): IMaybe<TFail>
2222
abstract match<M>(fn: IResultMatchPattern<TOk, TFail, M>): M
2323
abstract map<M>(fn: (val: TOk) => M): IResult<M, TFail>
@@ -50,8 +50,8 @@ export class OkResult<TOk, TFail> extends Result<TOk, TFail> {
5050
throw new ReferenceError('Cannot unwrap a success as a failure')
5151
}
5252

53-
maybeOk(): IMaybe<TOk> {
54-
return maybe(this.successValue)
53+
maybeOk(): IMaybe<NonNullable<TOk>> {
54+
return maybe(this.successValue as NonNullable<TOk>)
5555
}
5656

5757
maybeFail(): IMaybe<TFail> {
@@ -101,7 +101,7 @@ export class FailResult<TOk, TFail> extends Result<TOk, TFail> implements IResul
101101
return this.value
102102
}
103103

104-
maybeOk(): IMaybe<TOk> {
104+
maybeOk(): IMaybe<NonNullable<TOk>> {
105105
return none()
106106
}
107107

0 commit comments

Comments
 (0)