Skip to content

Commit

Permalink
feat: narrow down Result type after isOk or isErr
Browse files Browse the repository at this point in the history
  • Loading branch information
yifanwww committed Apr 14, 2024
1 parent a8e4b3c commit 81cf609
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
18 changes: 18 additions & 0 deletions src/__tests__/result.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ describe(`Test method \`${RustlikeResult.name}.prototype.${RustlikeResult.protot
expect(Err('Some error message').isOk()).toBe(false);
});

// eslint-disable-next-line jest/expect-expect
it('should narrow down result type', () => {
const result = Ok<number, string>(1);
if (result.isOk()) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const expected: Result<number, never> = result;
}
});

it('should have correct examples doc', () => {
function examples() {
const x: Result<number, string> = Ok(2);
Expand Down Expand Up @@ -153,6 +162,15 @@ describe(`Test method \`${RustlikeResult.name}.prototype.${RustlikeResult.protot
expect(Err('Some error message').isErr()).toBe(true);
});

// eslint-disable-next-line jest/expect-expect
it('should narrow down result type', () => {
const result = Err<number, string>('Some error message');
if (result.isErr()) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const expected: Result<never, string> = result;
}
});

it('should have correct examples doc', () => {
function examples() {
const x: Result<number, string> = Ok(-3);
Expand Down
4 changes: 2 additions & 2 deletions src/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class RustlikeResult<T, E> implements Result<T, E> {
*
* ref: https://doc.rust-lang.org/std/result/enum.Result.html#method.is_ok
*/
isOk(): boolean {
isOk(): this is Result<T, never> {
return this._type === 'ok';
}

Expand Down Expand Up @@ -125,7 +125,7 @@ export class RustlikeResult<T, E> implements Result<T, E> {
*
* ref: https://doc.rust-lang.org/std/result/enum.Result.html#method.is_err
*/
isErr(): boolean {
isErr(): this is Result<never, E> {
return this._type === 'err';
}

Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface Result<T, E> {
*
* ref: https://doc.rust-lang.org/std/result/enum.Result.html#method.is_ok
*/
isOk(): boolean;
isOk(): this is Result<T, never>;

/**
* Returns `true` if the result is `Ok` and the value inside of it matches a predicate.
Expand Down Expand Up @@ -91,7 +91,7 @@ export interface Result<T, E> {
*
* ref: https://doc.rust-lang.org/std/result/enum.Result.html#method.is_err
*/
isErr(): boolean;
isErr(): this is Result<never, E>;

/**
* Returns `true` if the result is `Err` and the value inside of it matches a predicate.
Expand Down

0 comments on commit 81cf609

Please sign in to comment.