Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix map return type #7

Merged
merged 2 commits into from
Feb 8, 2024
Merged
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
1 change: 0 additions & 1 deletion .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ jobs:
- run: pnpm install --frozen-lockfile
- run: pnpm run format:check
- run: pnpm run lint
- run: pnpm run typecheck
- run: pnpm run build
- run: pnpm test
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"sideEffects": false,
"files": [
"dist",
"!dist/**/*.spec.*",
"src",
"!src/**/*.spec.ts"
],
Expand All @@ -25,7 +26,6 @@
"format": "prettier --write .",
"format:check": "prettier --check .",
"lint": "eslint --max-warnings 0 .",
"typecheck": "tsc -p tsconfig.node.json --noEmit",
"test": "vitest",
"build": "tsc"
},
Expand Down
10 changes: 10 additions & 0 deletions src/async.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ describe('Async', () => {
});

it('#mapReady', () => {
expectTypeOf(Async.Pending.mapReady(() => 42)).toEqualTypeOf<
Async<number>
>();

const matched = Async.Pending.mapReady(() => 42).match({
Pending: () => 0,
Ready: value => value,
Expand All @@ -34,6 +38,12 @@ describe('Async', () => {
});

it('#flatMapReady', () => {
expectTypeOf(
Async.Ready('anything').flatMapReady(s =>
s === 'something' ? Async.Ready(s) : Async.Pending,
),
).toEqualTypeOf<Async<string>>();

const matched = Async.Ready('anything')
.flatMapReady(s => (s === 'something' ? Async.Ready(s) : Async.Pending))
.match({
Expand Down
2 changes: 1 addition & 1 deletion src/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class Async<T> {
* values.
* This is used to implement most of the operations
*/
match<U, P>(arms: { Pending: () => P; Ready: (value: T) => U }) {
match<U>(arms: { Pending: () => U; Ready: (value: T) => U }) {
return this.#state.isReady ? arms.Ready(this.#state.value) : arms.Pending();
}

Expand Down
8 changes: 8 additions & 0 deletions src/maybe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ describe('Maybe', () => {
});

it('#mapSome', () => {
expectTypeOf(Maybe.None.mapSome(() => 42)).toEqualTypeOf<Maybe<number>>();

const matched = Maybe.None.mapSome(() => 42).match({
None: () => 0,
Some: value => value,
Expand All @@ -34,6 +36,12 @@ describe('Maybe', () => {
});

it('#flatMapSome', () => {
expectTypeOf(
Maybe.None.flatMapSome(s =>
s === 'something' ? Maybe.None : Maybe.Some('asdfasdf'),
),
).toEqualTypeOf<Maybe<string>>();

const matched = Maybe.Some('anything')
.flatMapSome(s => (s === 'something' ? Maybe.Some(s) : Maybe.None))
.match({
Expand Down
2 changes: 1 addition & 1 deletion src/maybe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class Maybe<T> {
* value.
* This is used to implement most of the operations
*/
match<U, N>(arms: { None: () => N; Some: (value: T) => U }) {
match<U>(arms: { None: () => U; Some: (value: T) => U }) {
return this.#state.isSome ? arms.Some(this.#state.value) : arms.None();
}

Expand Down
4 changes: 2 additions & 2 deletions src/result.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ describe('Result', () => {
const matchedSuccess = Result.Ok(42)
.getOk()
.match({
None: () => 'nothing',
None: () => null,
Some: value => value,
});

expectTypeOf(matchedSuccess).toEqualTypeOf<number | string>();
expectTypeOf(matchedSuccess).toEqualTypeOf<number | null>();
expect(matchedSuccess).toBe(42);

const matchedFailure = Result.Err('Something went wrong')
Expand Down
6 changes: 3 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{
"files": ["src/index.ts"],
"exclude": ["src/**/*.spec.ts"],
"include": ["src"],
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
"lib": ["esnext"],
"lib": ["dom", "dom.iterable", "esnext"],
"module": "esnext",
"moduleResolution": "bundler",
"noErrorTruncation": true,
Expand All @@ -16,6 +15,7 @@
"noUnusedLocals": true,
"outDir": "dist",
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"target": "esnext"
Expand Down
20 changes: 0 additions & 20 deletions tsconfig.node.json

This file was deleted.

Loading