Skip to content

Commit

Permalink
Improve types of Result.{isOk, isErr, value}
Browse files Browse the repository at this point in the history
  • Loading branch information
ysulyma committed Nov 8, 2023
1 parent ea8ea36 commit 3ae39bf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

This document outlines the changes from version to version.

## 2.2.1

- make `Result.isOk()` and `Result.isErr()` narrow the type of `Result.value()`

## 2.2.0

- Added ESM support

## 2.1.1

- Added `Result.value()`
- Added `Result.value()`
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pratica",
"version": "2.2.0",
"version": "2.2.1",
"description": "Functional Programming for Pragmatists",
"main": "dist/index.cjs",
"module": "dist/index.esm.js",
Expand Down
23 changes: 17 additions & 6 deletions src/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,19 @@ export type Result<O, E> = {
}) => A | B
toMaybe: () => Maybe<O>
inspect: () => string
isErr: () => boolean
isOk: () => boolean,
value: () => O | E
}
// @ts-expect-error TS complains but this does work, https://github.com/microsoft/TypeScript/issues/51948
isErr: () => this is {__type: "Err"}

Check failure on line 18 in src/result.ts

View workflow job for this annotation

GitHub Actions / build (14.x)

A 'this' type is available only in a non-static member of a class or interface.

Check failure on line 18 in src/result.ts

View workflow job for this annotation

GitHub Actions / build (15.x)

A 'this' type is available only in a non-static member of a class or interface.

Check failure on line 18 in src/result.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

A 'this' type is available only in a non-static member of a class or interface.
// @ts-expect-error TS complains but this does work, https://github.com/microsoft/TypeScript/issues/51948
isOk: () => this is {__type: "Ok"}

Check failure on line 20 in src/result.ts

View workflow job for this annotation

GitHub Actions / build (14.x)

A 'this' type is available only in a non-static member of a class or interface.

Check failure on line 20 in src/result.ts

View workflow job for this annotation

GitHub Actions / build (15.x)

A 'this' type is available only in a non-static member of a class or interface.

Check failure on line 20 in src/result.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

A 'this' type is available only in a non-static member of a class or interface.
} & ({
/** @deprecated This is for type magic, do not use */
__type: "Ok";
value: () => O;
} | {
/** @deprecated This is for type magic, do not use */
__type: "Err";
value: () => E;
});


const _Ok = <O>(arg: O): Result<O, any> => ({
Expand All @@ -33,7 +42,8 @@ const _Ok = <O>(arg: O): Result<O, any> => ({
inspect: () => `Ok(${arg})`,
isErr: () => false,
isOk: () => true,
value: () => arg
value: () => arg,
__type: "Ok"
})

const _Err = <E>(arg: E): Result<any, E> => ({
Expand All @@ -49,7 +59,8 @@ const _Err = <E>(arg: E): Result<any, E> => ({
inspect: () => `Err(${arg})`,
isErr: () => true,
isOk: () => false,
value: () => arg
value: () => arg,
__type: "Err"
})

export function Ok(): Result<any, any>
Expand Down

0 comments on commit 3ae39bf

Please sign in to comment.