Skip to content

Commit fcb47a9

Browse files
committed
fix: fix/improve resolver types
1 parent 89d7f58 commit fcb47a9

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

src/object-merge/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ export type PathSegments = Array<string>
1717

1818
export type ResolveContext = {}
1919

20-
export type ResolveMethod = (
20+
export type ResolveMethod<T = ResolveContext> = (
2121
options: Array<any>,
22-
contexts: Array<ResolveContext>,
22+
contexts: Array<T>,
2323
active: MergedObjectValue,
2424
key: string | number | symbol,
2525
pathSegments: PathSegments,

src/resolvers/deepest.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { ResolveContext, ResolveMethod } from '../object-merge'
2-
import { MetaResolveContext } from '../types'
3-
import { resolveOption } from '.'
1+
import type { MetaResolveContext } from '../types'
2+
import { resolveOption } from './index'
43

54
type MergeResolveContextDeepest = MetaResolveContext & {
65
depth: number
@@ -24,9 +23,12 @@ export function setup (context: MergeResolveContextDeepest): void {
2423
context.depth = depth
2524
}
2625

27-
export const resolve: ResolveMethod = resolveOption((acc: any, context: ResolveContext) => {
28-
const { depth } = (context as unknown as MergeResolveContextDeepest)
29-
if (!acc || depth > acc) {
30-
return acc
26+
export const resolve = resolveOption<number, MergeResolveContextDeepest>((currentValue, context) => {
27+
const { depth } = context
28+
29+
if (!currentValue || depth > currentValue) {
30+
return depth
3131
}
32+
33+
return currentValue
3234
})

src/resolvers/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { ResolveContext, ResolveMethod } from '../object-merge'
1+
import type { ResolveContext, ResolveMethod } from '../object-merge'
22

3-
export type ResolveOptionReducer = (accumulator: any, context: ResolveContext) => ResolveMethod
3+
export interface ResolveOptionPredicament<T, U> {
4+
(currentValue: T | undefined, context: U): T
5+
}
46

5-
export const resolveOption: (predicament: ResolveOptionReducer) => ResolveMethod = predicament => (options, contexts) => {
7+
export const resolveOption = <T, U = ResolveContext>(predicament: ResolveOptionPredicament<T, U>, initialValue?: T): ResolveMethod<U> => (options, contexts) => {
68
let resolvedIndex = -1
79

8-
contexts.reduce((acc: ResolveContext | undefined, context, index) => {
10+
contexts.reduce((acc, context, index) => {
911
const retval = predicament(acc, context)
1012

1113
if (retval !== acc) {
@@ -14,7 +16,7 @@ export const resolveOption: (predicament: ResolveOptionReducer) => ResolveMethod
1416
}
1517

1618
return acc
17-
}, undefined)
19+
}, initialValue)
1820

1921
if (resolvedIndex > -1) {
2022
return options[resolvedIndex]

0 commit comments

Comments
 (0)