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

[dsch] adding the type-hidden properties error to Ok and value to Err… #6

Merged
merged 2 commits into from
Sep 26, 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
4 changes: 2 additions & 2 deletions examples/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ import { Result, ok, err } from 'okerr-ts/base';
}

{
const okResult: Result<number, 'NOT_FOUND'> = ok(42);
const errResult: Result<string, 'ERR_NOT_FOUND'> = err('ERR_NOT_FOUND');
const okResult = ok(42) as Result<number, 'NOT_FOUND'>;
const errResult = err('ERR_NOT_FOUND') as Result<string, 'ERR_NOT_FOUND'>;
DScheglov marked this conversation as resolved.
Show resolved Hide resolved

const value = okResult.unpack(); // 42
const error = errResult.unpack(); // 'ERR_NOT_FOUND'
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,19 @@
},
"license": "MIT",
"devDependencies": {
"@type-challenges/utils": "^0.1.1",
"@types/node": "^20.10.5",
"@eslint/js": "^9.7.0",
"@type-challenges/utils": "^0.1.1",
"@types/eslint": "^9.6.0",
"@types/node": "^20.10.5",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.2.1",
"typescript-eslint": "^7.17.0",
"jest": "^29.7.0",
"terser": "^5.33.0",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.2",
"tsup": "^8.3.0",
"typescript": "^5.6.2"
"typescript": "^5.6.2",
"typescript-eslint": "^7.17.0"
},
"exports": {
".": {
Expand Down
6 changes: 6 additions & 0 deletions src/Err.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ export class ErrImpl<E> implements Err<E> {
return true;
}

get value(): never {
throw new TypeError('Cannot access `value` on an Err instance.', {
cause: this,
});
}

map(): Result<never, E> {
return this;
}
Expand Down
6 changes: 6 additions & 0 deletions src/Ok.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ export class OkImpl<T> implements Ok<T> {
return false;
}

get error(): never {
throw new TypeError('Cannot access `error` on an Ok instance.', {
cause: this,
});
}
DScheglov marked this conversation as resolved.
Show resolved Hide resolved

map<S>(fn: (value: T) => S): Result<S, never> {
return new OkImpl(fn(this.value));
}
Expand Down
28 changes: 28 additions & 0 deletions src/result.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,34 @@ describe('Result', () => {
});
});

describe('Ok.value', () => {
it('returns the value for an Ok result', () => {
expect(ok('foo').value).toBe('foo');
});

it('throws a TypeError for an Err result', () => {
expect(() => (err('foo') as any).value).toThrowError(
new TypeError('Cannot access `value` on an Err instance.', {
cause: err('foo'),
}),
);
});
});

describe('Err.error', () => {
it('returns the error for an Err result', () => {
expect(err('foo').error).toBe('foo');
});

it('throws a TypeError for an Ok result', () => {
expect(() => (ok('foo') as any).error).toThrowError(
new TypeError('Cannot access `error` on an Ok instance.', {
cause: ok('foo'),
}),
);
});
});

describe('isErr', () => {
it('should return true for an Err result', () => {
expect(Guards.isErr(err('foo'))).toBe(true);
Expand Down
5 changes: 3 additions & 2 deletions tsup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const entries = [
];

export default defineConfig(
entries.map(([entry, globalName]) => ({
entries.map(([entry, globalName, options]) => ({
entry,
globalName,
format: ['iife'],
Expand All @@ -24,9 +24,10 @@ export default defineConfig(
};
},
dts: true,
minify: false,
minify: 'terser',
splitting: false,
sourcemap: true,
outDir: 'dist',
...options,
})),
);
Loading