Skip to content

Commit

Permalink
enable strict mode in tsconfig (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
sodiray committed Nov 5, 2022
1 parent 18de6c4 commit 93d7ce4
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
6 changes: 3 additions & 3 deletions src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ export const intersects = <T, K extends string | number | symbol>(
...acc,
[ident(item)]: true
}),
{} as Record<string | number | symbol, true>
{} as Record<string | number | symbol, boolean>
)
return listA.some(value => dictB[ident(value)])
}
Expand Down Expand Up @@ -326,7 +326,7 @@ export const merge = <T>(
const matched = others.find(o => matcher(r) === matcher(o))
if (matched) return [...acc, matched]
else return [...acc, r]
}, [])
}, [] as T[])
}

/**
Expand Down Expand Up @@ -402,7 +402,7 @@ export const diff = <T>(
...acc,
[identity(item)]: true
}),
{}
{} as Record<string | number | symbol, boolean>
)
return root.filter(a => !bKeys[identity(a)])
}
Expand Down
11 changes: 8 additions & 3 deletions src/async.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fork, list, sort } from './array'
import { fork, list, range, sort } from './array'

/**
* An async reduce function. Works like the
Expand Down Expand Up @@ -122,7 +122,7 @@ export const parallel = async <T, K>(
const [error, result] = await tryit(func)(next.item)
results.push({
error,
result,
result: result as K,
index: next.index
})
}
Expand Down Expand Up @@ -156,7 +156,7 @@ export const retry = async <TResponse>(
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]
Expand All @@ -166,6 +166,11 @@ export const retry = async <TResponse>(
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
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const toFloat = <T extends number | null = number>(
return isNaN(result) ? def : result
}

export const toInt = <T extends number | null>(
export const toInt = <T extends number | null = number>(
value: any,
defaultValue?: T
): number | T => {
Expand Down
18 changes: 11 additions & 7 deletions src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export const shake = <RemovedKeys extends string, T>(
filter: (value: any) => boolean = x => x === undefined
): Omit<T, RemovedKeys> => {
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] }
Expand All @@ -37,7 +38,8 @@ export const mapKeys = <
obj: Record<TKey, TValue>,
mapFunc: (key: TKey, value: TValue) => TNewKey
): Record<TNewKey, TValue> => {
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]
Expand All @@ -55,9 +57,10 @@ export const mapValues = <
TNewValue
>(
obj: Record<TKey, TValue>,
mapFunc: (value: TValue, key: string) => TNewValue
mapFunc: (value: TValue, key: TKey) => TNewValue
): Record<TKey, TNewValue> => {
return Object.keys(obj).reduce(
const keys = Object.keys(obj) as TKey[]
return keys.reduce(
(acc, key) => ({
...acc,
[key]: mapFunc(obj[key], key)
Expand Down Expand Up @@ -99,7 +102,8 @@ export const invert = <
obj: Record<TKey, TValue>
): Record<TValue, TKey> => {
if (!obj) return {} as Record<TValue, TKey>
return Object.keys(obj).reduce(
const keys = Object.keys(obj) as TKey[]
return keys.reduce(
(acc, key) => ({
...acc,
[obj[key]]: key
Expand All @@ -124,7 +128,7 @@ export const clone = <T extends object = object>(obj: T): T => {
return Object.getOwnPropertyNames(obj).reduce(
(acc, name) => ({
...acc,
[name]: obj[name]
[name]: obj[name as keyof T]
}),
{} as T
)
Expand Down Expand Up @@ -193,7 +197,7 @@ export const get = <T, K>(
value: T,
funcOrPath: ((t: T) => K) | string,
defaultValue: K | null = null
): K => {
): K | null => {
if (isFunction(funcOrPath)) {
try {
return (funcOrPath as Function)(value) ?? defaultValue
Expand Down
2 changes: 1 addition & 1 deletion src/tests/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions src/tests/async.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, [])
})

Expand Down Expand Up @@ -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)
Expand All @@ -166,7 +166,7 @@ describe('async module', () => {
{ rethrow: true }
)
})
} catch (err) {
} catch (err: any) {
error = err
}
assert.isNotNull(error)
Expand All @@ -183,7 +183,7 @@ describe('async module', () => {
{ rethrow: false }
)
})
} catch (err) {
} catch (err: any) {
error = err
}
assert.isNull(error)
Expand All @@ -196,7 +196,7 @@ describe('async module', () => {
throw new Error('soooo broken')
})
})
} catch (err) {
} catch (err: any) {
error = err
}
assert.isNull(error)
Expand Down
2 changes: 1 addition & 1 deletion src/tests/random.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
2 changes: 1 addition & 1 deletion src/tests/typed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
2 changes: 1 addition & 1 deletion src/typed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const isEqual = <TType>(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++) {
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}

0 comments on commit 93d7ce4

Please sign in to comment.