diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..3662b370 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "typescript.tsdk": "node_modules/typescript/lib" +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 305a0d80..d4104b08 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "neverthrow", - "version": "7.0.0", + "version": "7.0.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "neverthrow", - "version": "7.0.0", + "version": "7.0.1", "license": "MIT", "devDependencies": { "@babel/core": "7.24.7", @@ -9629,6 +9629,7 @@ "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.7.2.tgz", "integrity": "sha512-Mamb1iX2FDUpcTRzltPxgWMKy3fhg0TN378ylbktPGPK/99KbDtMQ4W1hwgsbPAsG3a0xKa1vmw4VKZQbkvz5A==", "dev": true, + "license": "Apache-2.0", "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" diff --git a/package.json b/package.json index b0bd0b45..f14747e6 100644 --- a/package.json +++ b/package.json @@ -9,11 +9,10 @@ "dist" ], "scripts": { - "test": "jest && npm run test-types", - "test-types": "tsc --noEmit -p ./tests/tsconfig.tests.json", + "test": "jest", "lint": "eslint ./src --ext .ts", "format": "prettier --write 'src/**/*.ts?(x)' && npm run lint -- --fix", - "typecheck": "tsc --noEmit", + "typecheck": "tsc -b", "clean": "rm -rf ./dist ./tmp", "build": "npm run clean && rollup --config && mv tmp/*.js dist", "prepack": "npm run build", diff --git a/src/result-async.ts b/src/result-async.ts index 8f926cb6..0324fe07 100644 --- a/src/result-async.ts +++ b/src/result-async.ts @@ -56,7 +56,8 @@ export class ResultAsync implements PromiseLike> { return new Err(errorFn ? errorFn(error) : error) } })(), - ) + // eslint-disable-next-line @typescript-eslint/no-explicit-any + ) as any } } diff --git a/src/result.ts b/src/result.ts index aeb23a6f..3a55d092 100644 --- a/src/result.ts +++ b/src/result.ts @@ -23,14 +23,16 @@ export namespace Result { fn: Fn, errorFn?: (e: unknown) => E, ): (...args: Parameters) => Result, E> { - return (...args) => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + return ((...args: any[]) => { try { const result = fn(...args) return ok(result) } catch (e) { return err(errorFn ? errorFn(e) : e) } - } + // eslint-disable-next-line @typescript-eslint/no-explicit-any + }) as any } export function combine< diff --git a/tests/index.test.ts b/tests/index.test.ts index b40ea259..f1f98886 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -333,15 +333,15 @@ describe('Result.fromThrowable', () => { // Added for issue #300 -- the test here is not so much that expectations are met as that the test compiles. it('Accepts an inner function which takes arguments', () => { - const hello = (fname: string): string => `hello, ${fname}`; - const safeHello = Result.fromThrowable(hello); + const hello = (fname: string): string => `hello, ${fname}` + const safeHello = Result.fromThrowable(hello) - const result = hello('Dikembe'); - const safeResult = safeHello('Dikembe'); + const result = hello('Dikembe') + const safeResult = safeHello('Dikembe') - expect(safeResult).toBeInstanceOf(Ok); - expect(result).toEqual(safeResult._unsafeUnwrap()); - }); + expect(safeResult).toBeInstanceOf(Ok) + expect(result).toEqual(safeResult._unsafeUnwrap()) + }) it('Creates a function that returns an err when the inner function throws', () => { const thrower = (): string => { @@ -376,7 +376,7 @@ describe('Result.fromThrowable', () => { }) it('has a top level export', () => { - expect(fromThrowable).toBe(Result.fromThrowable) + expect(fromThrowable).toBe(Result.fromThrowable) }) }) @@ -407,15 +407,15 @@ describe('Utils', () => { }) it('Combines heterogeneous lists', () => { - type HeterogenousList = [ Result, Result, Result ] - - const heterogenousList: HeterogenousList = [ - ok('Yooooo'), - ok(123), - ok(true), + type HeterogenousList = [ + Result, + Result, + Result, ] - type ExpecteResult = Result<[ string, number, boolean ], string | number | boolean> + const heterogenousList: HeterogenousList = [ok('Yooooo'), ok(123), ok(true)] + + type ExpecteResult = Result<[string, number, boolean], string | number | boolean> const result: ExpecteResult = Result.combine(heterogenousList) @@ -423,21 +423,18 @@ describe('Utils', () => { }) it('Does not destructure / concatenate arrays', () => { - type HomogenousList = [ - Result, - Result, - ] + type HomogenousList = [Result, Result] - const homogenousList: HomogenousList = [ - ok(['hello', 'world']), - ok([1, 2, 3]) - ] + const homogenousList: HomogenousList = [ok(['hello', 'world']), ok([1, 2, 3])] - type ExpectedResult = Result<[ string[], number[] ], boolean | string> + type ExpectedResult = Result<[string[], number[]], boolean | string> const result: ExpectedResult = Result.combine(homogenousList) - expect(result._unsafeUnwrap()).toEqual([ [ 'hello', 'world' ], [ 1, 2, 3 ]]) + expect(result._unsafeUnwrap()).toEqual([ + ['hello', 'world'], + [1, 2, 3], + ]) }) }) @@ -446,7 +443,7 @@ describe('Utils', () => { const asyncResultList = [okAsync(123), okAsync(456), okAsync(789)] const resultAsync: ResultAsync = ResultAsync.combine(asyncResultList) - + expect(resultAsync).toBeInstanceOf(ResultAsync) const result = await ResultAsync.combine(asyncResultList) @@ -481,14 +478,14 @@ describe('Utils', () => { okAsync('Yooooo'), okAsync(123), okAsync(true), - okAsync([ 1, 2, 3]), + okAsync([1, 2, 3]), ] - type ExpecteResult = Result<[ string, number, boolean, number[] ], string | number | boolean> + type ExpecteResult = Result<[string, number, boolean, number[]], string | number | boolean> const result: ExpecteResult = await ResultAsync.combine(heterogenousList) - expect(result._unsafeUnwrap()).toEqual(['Yooooo', 123, true, [ 1, 2, 3 ]]) + expect(result._unsafeUnwrap()).toEqual(['Yooooo', 123, true, [1, 2, 3]]) }) }) }) @@ -518,15 +515,15 @@ describe('Utils', () => { }) it('Combines heterogeneous lists', () => { - type HeterogenousList = [ Result, Result, Result ] - - const heterogenousList: HeterogenousList = [ - ok('Yooooo'), - ok(123), - ok(true), + type HeterogenousList = [ + Result, + Result, + Result, ] - type ExpecteResult = Result<[ string, number, boolean ], (string | number | boolean)[]> + const heterogenousList: HeterogenousList = [ok('Yooooo'), ok(123), ok(true)] + + type ExpecteResult = Result<[string, number, boolean], (string | number | boolean)[]> const result: ExpecteResult = Result.combineWithAllErrors(heterogenousList) @@ -534,21 +531,18 @@ describe('Utils', () => { }) it('Does not destructure / concatenate arrays', () => { - type HomogenousList = [ - Result, - Result, - ] + type HomogenousList = [Result, Result] - const homogenousList: HomogenousList = [ - ok(['hello', 'world']), - ok([1, 2, 3]) - ] + const homogenousList: HomogenousList = [ok(['hello', 'world']), ok([1, 2, 3])] - type ExpectedResult = Result<[ string[], number[] ], (boolean | string)[]> + type ExpectedResult = Result<[string[], number[]], (boolean | string)[]> const result: ExpectedResult = Result.combineWithAllErrors(homogenousList) - expect(result._unsafeUnwrap()).toEqual([ [ 'hello', 'world' ], [ 1, 2, 3 ]]) + expect(result._unsafeUnwrap()).toEqual([ + ['hello', 'world'], + [1, 2, 3], + ]) }) }) describe('`ResultAsync.combineWithAllErrors`', () => { @@ -576,15 +570,15 @@ describe('Utils', () => { }) it('Combines heterogeneous lists', async () => { - type HeterogenousList = [ ResultAsync, ResultAsync, ResultAsync ] - - const heterogenousList: HeterogenousList = [ - okAsync('Yooooo'), - okAsync(123), - okAsync(true), + type HeterogenousList = [ + ResultAsync, + ResultAsync, + ResultAsync, ] - type ExpecteResult = Result<[ string, number, boolean ], [string, number, boolean]> + const heterogenousList: HeterogenousList = [okAsync('Yooooo'), okAsync(123), okAsync(true)] + + type ExpecteResult = Result<[string, number, boolean], [string, number, boolean]> const result: ExpecteResult = await ResultAsync.combineWithAllErrors(heterogenousList) @@ -832,7 +826,6 @@ describe('ResultAsync', () => { const okVal = okAsync(12) const errorCallback = jest.fn((_errVal) => errAsync('It is now a string')) - const result = await okVal.orElse(errorCallback) expect(result).toEqual(ok(12)) @@ -959,7 +952,7 @@ describe('ResultAsync', () => { return 12 }) - + const val = await example() expect(val.isErr()).toBe(true) @@ -969,9 +962,9 @@ describe('ResultAsync', () => { it('Accepts an error handler as a second argument', async () => { const example = ResultAsync.fromThrowable( () => Promise.reject('No!'), - (e) => new Error('Oops: ' + e) + (e) => new Error('Oops: ' + e), ) - + const val = await example() expect(val.isErr()).toBe(true) diff --git a/tests/tsconfig.tests.json b/tests/tsconfig.tests.json deleted file mode 100644 index d9bc7db2..00000000 --- a/tests/tsconfig.tests.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "compilerOptions": { - "target": "es2016", - "module": "ES2015", - "noImplicitAny": true, - "sourceMap": false, - "downlevelIteration": true, - "noUnusedLocals": false, - "noUnusedParameters": false, - "strictNullChecks": true, - "strictFunctionTypes": true, - "declaration": true, - "moduleResolution": "Node", - "baseUrl": "./src", - "lib": [ - "dom", - "es2016", - "es2017.object" - ], - "outDir": "dist", - }, - "include": [ - "./index.test.ts", - "./typecheck-tests.ts" - ], -} diff --git a/tsconfig.json b/tsconfig.json index 5149334f..f60e10ac 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,31 +1,26 @@ { "compilerOptions": { - "target": "es2015", - "module": "ESNext", - "moduleResolution": "Node", - "strict": false, - "noImplicitAny": true, - "sourceMap": false, - "noUnusedLocals": true, - "noUnusedParameters": true, - "strictNullChecks": true, - "strictFunctionTypes": true, - "declaration": true, - "baseUrl": "./src", - "lib": [ - "dom", - "es2016", - "es2017.object" - ], - "outDir": "dist", - "skipLibCheck": true, - "esModuleInterop": true + "target": "ES2015", + "module": "ESNext", + "moduleResolution": "Node", + "strict": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "lib": [ + "ES2016", + "ES2017.Object" + ], + "skipLibCheck": true, + "esModuleInterop": true, + "noEmit": true }, "include": [ - "src/**/*.ts" + "src/**/*.ts", ], - "exclude": [ - "node_modules", - "**/*.spec.ts" + "exclude": [], + "references": [ + { + "path": "./tsconfig.tests.json" + } ] -} +} \ No newline at end of file diff --git a/tsconfig.tests.json b/tsconfig.tests.json new file mode 100644 index 00000000..375575d4 --- /dev/null +++ b/tsconfig.tests.json @@ -0,0 +1,18 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "noUnusedLocals": false, + "noUnusedParameters": false, + "declaration": true, + "composite": true, + "outDir": "./tests/dist", + "noEmit": false + }, + "include": [ + "./src/**/*.ts", + "./tests/**/*.ts", + ], + "exclude": [ + "./tests/dist" + ] +} \ No newline at end of file