From a2b5086bf71dd0489004bd0fc31319ab49bafef5 Mon Sep 17 00:00:00 2001 From: yifanwww Date: Sun, 24 Mar 2024 00:16:16 +0800 Subject: [PATCH] feat: remove type limit of equal method --- README.md | 6 +++++- src/__tests__/result.test.ts | 4 ++++ src/result.ts | 2 +- src/types.ts | 2 +- 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 29f652f..d54a0d1 100644 --- a/README.md +++ b/README.md @@ -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); diff --git a/src/__tests__/result.test.ts b/src/__tests__/result.test.ts index 0522674..34a7d9f 100644 --- a/src/__tests__/result.test.ts +++ b/src/__tests__/result.test.ts @@ -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); }); }); diff --git a/src/result.ts b/src/result.ts index 1d9344a..94ec535 100644 --- a/src/result.ts +++ b/src/result.ts @@ -469,7 +469,7 @@ export class RustlikeResult implements Result { /** * Returns `true` if `self` equals to `other`. */ - equal(other: Result): boolean { + equal(other: Result): boolean { const isOk = this.isOk(); if (isOk !== other.isOk()) return false; return isOk diff --git a/src/types.ts b/src/types.ts index 3e1a01c..8a8ccd4 100644 --- a/src/types.ts +++ b/src/types.ts @@ -328,5 +328,5 @@ export interface Result { /** * Returns `true` if `self` equals to `other`. */ - equal(other: Result): boolean; + equal(other: Result): boolean; }