From 93d7ce4caed78d83a26e3784d489ad85655d1121 Mon Sep 17 00:00:00 2001 From: Ray Epps Date: Fri, 4 Nov 2022 20:19:18 -0600 Subject: [PATCH] enable strict mode in tsconfig (#139) --- package.json | 2 +- src/array.ts | 6 +++--- src/async.ts | 11 ++++++++--- src/number.ts | 2 +- src/object.ts | 18 +++++++++++------- src/tests/array.test.ts | 2 +- src/tests/async.test.ts | 10 +++++----- src/tests/random.test.ts | 2 +- src/tests/typed.test.ts | 2 +- src/typed.ts | 2 +- tsconfig.json | 3 ++- 11 files changed, 35 insertions(+), 25 deletions(-) diff --git a/package.json b/package.json index ecf5ab94..acd06a7e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "radash", - "version": "9.0.1", + "version": "9.0.2", "description": "Functional utility library - modern, simple, typed, powerful", "main": "dist/cjs/index.cjs", "module": "dist/esm/index.mjs", diff --git a/src/array.ts b/src/array.ts index d983ed60..fe1441f7 100644 --- a/src/array.ts +++ b/src/array.ts @@ -281,7 +281,7 @@ export const intersects = ( ...acc, [ident(item)]: true }), - {} as Record + {} as Record ) return listA.some(value => dictB[ident(value)]) } @@ -326,7 +326,7 @@ export const merge = ( const matched = others.find(o => matcher(r) === matcher(o)) if (matched) return [...acc, matched] else return [...acc, r] - }, []) + }, [] as T[]) } /** @@ -402,7 +402,7 @@ export const diff = ( ...acc, [identity(item)]: true }), - {} + {} as Record ) return root.filter(a => !bKeys[identity(a)]) } diff --git a/src/async.ts b/src/async.ts index b637b5c2..d2310c7b 100644 --- a/src/async.ts +++ b/src/async.ts @@ -1,4 +1,4 @@ -import { fork, list, sort } from './array' +import { fork, list, range, sort } from './array' /** * An async reduce function. Works like the @@ -122,7 +122,7 @@ export const parallel = async ( const [error, result] = await tryit(func)(next.item) results.push({ error, - result, + result: result as K, index: next.index }) } @@ -156,7 +156,7 @@ export const retry = async ( const times = options?.times ?? 3 const delay = options?.delay const backoff = options?.backoff ?? null - for (const i of list(1, times)) { + for (const i of range(1, times)) { const [err, result] = (await tryit(func)((err: any) => { throw { _exited: err } })) as [any, TResponse] @@ -166,6 +166,11 @@ export const retry = async ( if (delay) await sleep(delay) if (backoff) await sleep(backoff(i)) } + // Logically, we should never reach this + // code path. It makes the function meet + // strict mode requirements. + /* istanbul ignore next */ + return undefined as unknown as TResponse } /** diff --git a/src/number.ts b/src/number.ts index 8d48fa59..209ea845 100644 --- a/src/number.ts +++ b/src/number.ts @@ -10,7 +10,7 @@ export const toFloat = ( return isNaN(result) ? def : result } -export const toInt = ( +export const toInt = ( value: any, defaultValue?: T ): number | T => { diff --git a/src/object.ts b/src/object.ts index abaf393c..df49dd23 100644 --- a/src/object.ts +++ b/src/object.ts @@ -18,7 +18,8 @@ export const shake = ( filter: (value: any) => boolean = x => x === undefined ): Omit => { if (!obj) return {} as T - return Object.keys(obj).reduce((acc, key) => { + const keys = Object.keys(obj) as (keyof T)[] + return keys.reduce((acc, key) => { if (filter(obj[key])) { return acc } else return { ...acc, [key]: obj[key] } @@ -37,7 +38,8 @@ export const mapKeys = < obj: Record, mapFunc: (key: TKey, value: TValue) => TNewKey ): Record => { - return Object.keys(obj).reduce( + const keys = Object.keys(obj) as TKey[] + return keys.reduce( (acc, key) => ({ ...acc, [mapFunc(key as TKey, obj[key])]: obj[key] @@ -55,9 +57,10 @@ export const mapValues = < TNewValue >( obj: Record, - mapFunc: (value: TValue, key: string) => TNewValue + mapFunc: (value: TValue, key: TKey) => TNewValue ): Record => { - return Object.keys(obj).reduce( + const keys = Object.keys(obj) as TKey[] + return keys.reduce( (acc, key) => ({ ...acc, [key]: mapFunc(obj[key], key) @@ -99,7 +102,8 @@ export const invert = < obj: Record ): Record => { if (!obj) return {} as Record - return Object.keys(obj).reduce( + const keys = Object.keys(obj) as TKey[] + return keys.reduce( (acc, key) => ({ ...acc, [obj[key]]: key @@ -124,7 +128,7 @@ export const clone = (obj: T): T => { return Object.getOwnPropertyNames(obj).reduce( (acc, name) => ({ ...acc, - [name]: obj[name] + [name]: obj[name as keyof T] }), {} as T ) @@ -193,7 +197,7 @@ export const get = ( value: T, funcOrPath: ((t: T) => K) | string, defaultValue: K | null = null -): K => { +): K | null => { if (isFunction(funcOrPath)) { try { return (funcOrPath as Function)(value) ?? defaultValue diff --git a/src/tests/array.test.ts b/src/tests/array.test.ts index 1c533f37..b243421e 100644 --- a/src/tests/array.test.ts +++ b/src/tests/array.test.ts @@ -227,7 +227,7 @@ describe('array module', () => { assert.deepEqual(result, ['hello', 'oh']) }) test('does not fail on empty input list', () => { - const list = [] + const list: any[] = [] const result = _.select( list, (x: any) => x.word, diff --git a/src/tests/async.test.ts b/src/tests/async.test.ts index 72cca209..d5427cde 100644 --- a/src/tests/async.test.ts +++ b/src/tests/async.test.ts @@ -32,7 +32,7 @@ describe('async module', () => { }) test('handles null input', async () => { - const result = await _.map(null, async () => '') + const result = await _.map(null as unknown as unknown[], async () => '') assert.deepEqual(result, []) }) @@ -149,7 +149,7 @@ describe('async module', () => { await _.defer(async () => { throw new Error('soooo broken') }) - } catch (err) { + } catch (err: any) { error = err } assert.isNotNull(error) @@ -166,7 +166,7 @@ describe('async module', () => { { rethrow: true } ) }) - } catch (err) { + } catch (err: any) { error = err } assert.isNotNull(error) @@ -183,7 +183,7 @@ describe('async module', () => { { rethrow: false } ) }) - } catch (err) { + } catch (err: any) { error = err } assert.isNull(error) @@ -196,7 +196,7 @@ describe('async module', () => { throw new Error('soooo broken') }) }) - } catch (err) { + } catch (err: any) { error = err } assert.isNull(error) diff --git a/src/tests/random.test.ts b/src/tests/random.test.ts index e778525a..e38ca5b0 100644 --- a/src/tests/random.test.ts +++ b/src/tests/random.test.ts @@ -71,7 +71,7 @@ describe('random module', () => { assert.include('abc', result!.id) }) test('returns null given empty input', () => { - const list = [] + const list: unknown[] = [] const result = _.draw(list) assert.isNull(result) }) diff --git a/src/tests/typed.test.ts b/src/tests/typed.test.ts index db5bfe6b..af50bceb 100644 --- a/src/tests/typed.test.ts +++ b/src/tests/typed.test.ts @@ -365,7 +365,7 @@ describe('typed module', () => { assert.isFalse(result) }) test('returns false for empty array', () => { - const input = [] + const input: unknown[] = [] const result = _.isSymbol(input) assert.isFalse(result) }) diff --git a/src/typed.ts b/src/typed.ts index 0b966dbe..ea8e85b5 100644 --- a/src/typed.ts +++ b/src/typed.ts @@ -69,7 +69,7 @@ export const isEqual = (x: TType, y: TType): boolean => { ) { return false } - const keysX = Reflect.ownKeys(x as unknown as object) + const keysX = Reflect.ownKeys(x as unknown as object) as (keyof typeof x)[] const keysY = Reflect.ownKeys(y as unknown as object) if (keysX.length !== keysY.length) return false for (let i = 0; i < keysX.length; i++) { diff --git a/tsconfig.json b/tsconfig.json index 3baa44fa..147a4de5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,9 +5,10 @@ "target": "es2020", "lib": ["es2020"], "esModuleInterop": true, + "strict": true }, "include": [ "src/**/*.ts" ], - "exclude": ["node_modules", "dist", "src/tests"] + "exclude": ["node_modules", "dist"] } \ No newline at end of file