From 9a30242d070b67f52af28b7e431f9f8a7cf8e7c4 Mon Sep 17 00:00:00 2001 From: Matt Pocock Date: Tue, 20 Aug 2024 09:02:38 +0100 Subject: [PATCH 1/4] Upgraded TS and moved to strict: true --- .vscode/settings.json | 3 +++ package-lock.json | 21 +++++++++++---------- package.json | 6 +++--- src/result-async.ts | 3 ++- src/result.ts | 6 ++++-- tsconfig.json | 42 +++++++++++++++++++----------------------- 6 files changed, 42 insertions(+), 39 deletions(-) create mode 100644 .vscode/settings.json 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..d9be47e9 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", @@ -31,7 +31,7 @@ "ts-jest": "27.1.5", "ts-toolbelt": "9.6.0", "tslib": "^2.4.0", - "typescript": "4.7.2" + "typescript": "5.0.2" }, "engines": { "node": ">=18" @@ -9625,16 +9625,17 @@ } }, "node_modules/typescript": { - "version": "4.7.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.7.2.tgz", - "integrity": "sha512-Mamb1iX2FDUpcTRzltPxgWMKy3fhg0TN378ylbktPGPK/99KbDtMQ4W1hwgsbPAsG3a0xKa1vmw4VKZQbkvz5A==", + "version": "5.0.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.0.2.tgz", + "integrity": "sha512-wVORMBGO/FAs/++blGNeAVdbNKtIh1rbBL2EyQ1+J9lClJ93KiiKe8PmFIVdXhHcyv44SL9oglmfeSsndo0jRw==", "dev": true, + "license": "Apache-2.0", "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" }, "engines": { - "node": ">=4.2.0" + "node": ">=12.20" } }, "node_modules/undici-types": { @@ -17155,9 +17156,9 @@ } }, "typescript": { - "version": "4.7.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.7.2.tgz", - "integrity": "sha512-Mamb1iX2FDUpcTRzltPxgWMKy3fhg0TN378ylbktPGPK/99KbDtMQ4W1hwgsbPAsG3a0xKa1vmw4VKZQbkvz5A==", + "version": "5.0.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.0.2.tgz", + "integrity": "sha512-wVORMBGO/FAs/++blGNeAVdbNKtIh1rbBL2EyQ1+J9lClJ93KiiKe8PmFIVdXhHcyv44SL9oglmfeSsndo0jRw==", "dev": true }, "undici-types": { diff --git a/package.json b/package.json index b0bd0b45..f83d8c00 100644 --- a/package.json +++ b/package.json @@ -10,10 +10,10 @@ ], "scripts": { "test": "jest && npm run test-types", - "test-types": "tsc --noEmit -p ./tests/tsconfig.tests.json", + "test-types": "tsc -p ./tests/tsconfig.tests.json", "lint": "eslint ./src --ext .ts", "format": "prettier --write 'src/**/*.ts?(x)' && npm run lint -- --fix", - "typecheck": "tsc --noEmit", + "typecheck": "tsc", "clean": "rm -rf ./dist ./tmp", "build": "npm run clean && rollup --config && mv tmp/*.js dist", "prepack": "npm run build", @@ -52,7 +52,7 @@ "ts-jest": "27.1.5", "ts-toolbelt": "9.6.0", "tslib": "^2.4.0", - "typescript": "4.7.2" + "typescript": "5.0.2" }, "keywords": [ "typescript", 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/tsconfig.json b/tsconfig.json index 5149334f..2155d34a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,31 +1,27 @@ { "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": "Bundler", + "strict": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "baseUrl": "./src", + "lib": [ + "dom", + "es2016", + "es2017.object" + ], + "outDir": "dist", + "skipLibCheck": true, + "esModuleInterop": true, + "noEmit": true }, "include": [ "src/**/*.ts" ], "exclude": [ - "node_modules", - "**/*.spec.ts" + "node_modules", + "**/*.spec.ts" ] -} +} \ No newline at end of file From 37ef88ba48273b1a6bb790bfeea552ac5960158a Mon Sep 17 00:00:00 2001 From: Matt Pocock Date: Tue, 20 Aug 2024 09:14:24 +0100 Subject: [PATCH 2/4] Moved the tsconfig.test.json to be a reference of the root tsconfig, and added strict: true --- package.json | 5 +- tests/index.test.ts | 107 ++++++++++++++++++-------------------- tests/tsconfig.tests.json | 26 --------- tsconfig.json | 18 +++---- tsconfig.tests.json | 18 +++++++ 5 files changed, 78 insertions(+), 96 deletions(-) delete mode 100644 tests/tsconfig.tests.json create mode 100644 tsconfig.tests.json diff --git a/package.json b/package.json index f83d8c00..2fffec6c 100644 --- a/package.json +++ b/package.json @@ -9,11 +9,10 @@ "dist" ], "scripts": { - "test": "jest && npm run test-types", - "test-types": "tsc -p ./tests/tsconfig.tests.json", + "test": "jest", "lint": "eslint ./src --ext .ts", "format": "prettier --write 'src/**/*.ts?(x)' && npm run lint -- --fix", - "typecheck": "tsc", + "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/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 2155d34a..e18d656e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,27 +1,25 @@ { "compilerOptions": { - "target": "es2015", + "target": "es2022", "module": "ESNext", "moduleResolution": "Bundler", "strict": true, "noUnusedLocals": true, "noUnusedParameters": true, - "baseUrl": "./src", "lib": [ - "dom", - "es2016", - "es2017.object" + "es2022" ], - "outDir": "dist", "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 From 6771c47703d16ec5f587e6d0ea55a5223ebf4877 Mon Sep 17 00:00:00 2001 From: Matt Pocock Date: Tue, 20 Aug 2024 09:19:23 +0100 Subject: [PATCH 3/4] Fixed CI by downgrading TS again --- package-lock.json | 16 ++++++++-------- package.json | 2 +- tsconfig.json | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/package-lock.json b/package-lock.json index d9be47e9..d4104b08 100644 --- a/package-lock.json +++ b/package-lock.json @@ -31,7 +31,7 @@ "ts-jest": "27.1.5", "ts-toolbelt": "9.6.0", "tslib": "^2.4.0", - "typescript": "5.0.2" + "typescript": "4.7.2" }, "engines": { "node": ">=18" @@ -9625,9 +9625,9 @@ } }, "node_modules/typescript": { - "version": "5.0.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.0.2.tgz", - "integrity": "sha512-wVORMBGO/FAs/++blGNeAVdbNKtIh1rbBL2EyQ1+J9lClJ93KiiKe8PmFIVdXhHcyv44SL9oglmfeSsndo0jRw==", + "version": "4.7.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.7.2.tgz", + "integrity": "sha512-Mamb1iX2FDUpcTRzltPxgWMKy3fhg0TN378ylbktPGPK/99KbDtMQ4W1hwgsbPAsG3a0xKa1vmw4VKZQbkvz5A==", "dev": true, "license": "Apache-2.0", "bin": { @@ -9635,7 +9635,7 @@ "tsserver": "bin/tsserver" }, "engines": { - "node": ">=12.20" + "node": ">=4.2.0" } }, "node_modules/undici-types": { @@ -17156,9 +17156,9 @@ } }, "typescript": { - "version": "5.0.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.0.2.tgz", - "integrity": "sha512-wVORMBGO/FAs/++blGNeAVdbNKtIh1rbBL2EyQ1+J9lClJ93KiiKe8PmFIVdXhHcyv44SL9oglmfeSsndo0jRw==", + "version": "4.7.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.7.2.tgz", + "integrity": "sha512-Mamb1iX2FDUpcTRzltPxgWMKy3fhg0TN378ylbktPGPK/99KbDtMQ4W1hwgsbPAsG3a0xKa1vmw4VKZQbkvz5A==", "dev": true }, "undici-types": { diff --git a/package.json b/package.json index 2fffec6c..f14747e6 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,7 @@ "ts-jest": "27.1.5", "ts-toolbelt": "9.6.0", "tslib": "^2.4.0", - "typescript": "5.0.2" + "typescript": "4.7.2" }, "keywords": [ "typescript", diff --git a/tsconfig.json b/tsconfig.json index e18d656e..aa1dbee8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "target": "es2022", "module": "ESNext", - "moduleResolution": "Bundler", + "moduleResolution": "Node", "strict": true, "noUnusedLocals": true, "noUnusedParameters": true, From 276b3b64c199972886b226d79eee54ef648cf36d Mon Sep 17 00:00:00 2001 From: Matt Pocock Date: Tue, 20 Aug 2024 09:21:34 +0100 Subject: [PATCH 4/4] Reverted some tsconfig.json changes --- tsconfig.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tsconfig.json b/tsconfig.json index aa1dbee8..f60e10ac 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,13 +1,14 @@ { "compilerOptions": { - "target": "es2022", + "target": "ES2015", "module": "ESNext", "moduleResolution": "Node", "strict": true, "noUnusedLocals": true, "noUnusedParameters": true, "lib": [ - "es2022" + "ES2016", + "ES2017.Object" ], "skipLibCheck": true, "esModuleInterop": true,