Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/result/src/lib/Option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ export class Option<T, Exists extends boolean = boolean> {
*
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.map}
*/
public map<U>(cb: (value: T) => U): If<Exists, Some<U>, None> {
public map<U>(cb: (value: T) => U): Option<U, Exists> {
// @ts-expect-error Complex types
return this.match({ some: (value) => some(cb(value)), none: returnThis });
}

Expand All @@ -250,7 +251,8 @@ export class Option<T, Exists extends boolean = boolean> {
*
* @note This is an extension not supported in Rust
*/
public mapInto<OutputOption extends AnyOption>(cb: (value: T) => OutputOption): If<Exists, OutputOption, None> {
public mapInto<OutputOption extends AnyOption>(cb: (value: T) => OutputOption): OutputOption {
// @ts-expect-error Complex types
return this.match({ some: (value) => cb(value), none: returnThis });
}

Expand Down Expand Up @@ -504,7 +506,8 @@ export class Option<T, Exists extends boolean = boolean> {
*
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.and_then}
*/
public andThen<OutputOption extends AnyOption>(cb: (value: T) => OutputOption): If<Exists, OutputOption, None> {
public andThen<OutputOption extends AnyOption>(cb: (value: T) => OutputOption): OutputOption {
// @ts-expect-error Complex types
return this.match({ some: (value) => cb(value), none: returnThis });
}

Expand Down
3 changes: 2 additions & 1 deletion packages/result/src/lib/Result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,8 @@ export class Result<T, E, const Success extends boolean = boolean> {
*
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.and_then}
*/
public andThen<OutputResult extends AnyResult>(cb: (value: T) => OutputResult): If<Success, OutputResult, Err<E>> {
public andThen<OutputResult extends AnyResult>(cb: (value: T) => OutputResult): OutputResult {
// @ts-expect-error Complex types
return this.match({ ok: (value) => cb(value), err: returnThis });
}

Expand Down
22 changes: 20 additions & 2 deletions packages/result/tests/Option.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,15 @@ describe('Option', () => {
expect<None>(x.map(op)).toBe(none);
expect(op).not.toHaveBeenCalled();
});

test('GIVEN option THEN chain map THEN Return Option<string>', () => {
const x = Option.from(5);
const cb = vi.fn((value: number) => `${value}`);
const mapChain = x.map(cb);

expectTypeOf(mapChain).toMatchTypeOf<Option<string>>();
expect<Option<string>>(mapChain).toEqual(some('5'));
});
});

describe('mapInto', () => {
Expand All @@ -191,7 +200,7 @@ describe('Option', () => {
const x = none;
const op = vi.fn((value: string) => some(value.length));

expect<None>(x.mapInto(op)).toBe(none);
expect(x.mapInto(op)).toBe(none);
expect(op).not.toHaveBeenCalled();
});
});
Expand Down Expand Up @@ -406,9 +415,18 @@ describe('Option', () => {
const x = none;
const op = vi.fn(cb);

expect<typeof x>(x.andThen(op)).toBe(none);
expect(x.andThen(op)).toBe(none);
expect(op).not.toHaveBeenCalled();
});

test('GIVEN option THEN andThen and chain map THEN Return Option<number>', () => {
const x = Option.from(5);
const cb = vi.fn((value: number) => some(`${value}`));
const mapChain = x.andThen(cb).map((value) => Number(value));

expectTypeOf(mapChain).toMatchTypeOf<Option<number>>();
expect<Option<number>>(mapChain).toEqual(some(5));
});
});

describe('or', () => {
Expand Down
11 changes: 10 additions & 1 deletion packages/result/tests/Result.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,9 +533,18 @@ describe('Result', () => {
const x = err('not a number');
const op = vi.fn(cb);

expect<typeof x>(x.andThen(op)).toBe(x);
expect(x.andThen(op)).toBe(x);
expect(op).not.toHaveBeenCalled();
});

test('GIVEN ok THEN chain andThen and map THEN returns Ok<number, string>', () => {
const x = Result.from<number, Error>(1);
const cb = vi.fn((value: number) => ok(`${value}`));
const mapChain = x.andThen(cb).map((value) => Number(value));

expectTypeOf(mapChain).toMatchTypeOf<Result<number, string>>();
expect<Result<number, string>>(mapChain).toEqual(ok(1));
});
});

describe('or', () => {
Expand Down