Skip to content

Commit

Permalink
feat: remove type limit of equal method
Browse files Browse the repository at this point in the history
  • Loading branch information
yifanwww committed Mar 23, 2024
1 parent 3b45e5b commit a2b5086
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,13 @@ const result = await Ok(1)

You can not just use `===` or `==` to compare `Result`, so `Result` itself provides an method call `equal` for that.

```javascript
```js
expect(Ok(1).equal(Ok(1))).toBe(true);
expect(Ok(1).equal(Ok(2))).toBe(false);
expect(Ok(1).equal(Ok(2))).toBe(false);
expect(Ok('hello').equal(Ok('hello'))).toBe(true);
expect(Ok('hello').equal(Ok('hello world'))).toBe(false);
expect(Ok(1).equal(Ok('hello world'))).toBe(false);

expect(Ok({ foo: 1 }).equal(Ok({ foo: 1 }))).toBe(false);
expect(Ok([1]).equal(Ok([1]))).toBe(false);
Expand Down
4 changes: 4 additions & 0 deletions src/__tests__/result.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1007,5 +1007,9 @@ describe(`Test method \`${RustlikeResult.name}.prototype.${RustlikeResult.protot
expect(Err(Err({ message: 'Some error message' })).equal(Err(Err({ message: 'Some error message' })))).toBe(
false,
);

expect(Ok(1).equal(Ok('hello world'))).toBe(false);
expect(Ok(1).equal(Err('error'))).toBe(false);
expect(Err('error').equal(Ok(1))).toBe(false);
});
});
2 changes: 1 addition & 1 deletion src/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ export class RustlikeResult<T, E> implements Result<T, E> {
/**
* Returns `true` if `self` equals to `other`.
*/
equal(other: Result<T, E>): boolean {
equal(other: Result<unknown, unknown>): boolean {
const isOk = this.isOk();
if (isOk !== other.isOk()) return false;
return isOk
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,5 +328,5 @@ export interface Result<T, E> {
/**
* Returns `true` if `self` equals to `other`.
*/
equal(other: Result<T, E>): boolean;
equal(other: Result<unknown, unknown>): boolean;
}

0 comments on commit a2b5086

Please sign in to comment.