Skip to content

Commit 129a44f

Browse files
committed
chore: improve typing
1 parent c36960e commit 129a44f

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

src/index.tsx

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,11 @@
11
import * as React from 'react'
2-
import { ComponentType, ReactNode, ReactElement } from 'react'
2+
import { ReactNode, ReactElement } from 'react'
33

44
const { values, keys, assign } = Object
55

6-
export type ChildrenFn<P = any> = (props: P) => ReactNode
6+
export type ChildrenFn<P> = (props: P) => ReactNode
77

8-
export type MapperValue =
9-
| ReactElement<any>
10-
| ChildrenFn<{
11-
renderProp?: ChildrenFn
12-
[key: string]: any
13-
}>
14-
15-
export type Mapper<R> = Record<keyof R, MapperValue>
16-
17-
export type RPC<RenderProps, Props = {}> = ComponentType<
18-
Props & {
19-
children: ChildrenFn<RenderProps>
20-
}
21-
>
22-
23-
function omit<R = object>(obj: any, omitProps: string[]): R {
8+
function omit<R = object>(omitProps: string[], obj: any): R {
249
const newObj = keys(obj)
2510
.filter((key: string): boolean => omitProps.indexOf(key) === -1)
2611
.reduce(
@@ -31,37 +16,57 @@ function omit<R = object>(obj: any, omitProps: string[]): R {
3116
return newObj as R
3217
}
3318

19+
function prop<T = any>(key: string, obj: any): T {
20+
return obj[key]
21+
}
22+
3423
const isFn = (val: any): boolean => Boolean(val) && typeof val === 'function'
3524

3625
const isValidRenderProp = (prop: ReactNode | ChildrenFn<any>): boolean =>
3726
React.isValidElement(prop) || isFn(prop)
3827

39-
export function adopt<RP extends Record<string, any>, P = {}>(
40-
mapper: Mapper<RP>
41-
): RPC<RP, P> {
28+
export type RPC<RP, P = {}> = React.SFC<
29+
P & {
30+
children: ChildrenFn<RP>
31+
}
32+
>
33+
34+
export type MapperComponent<RP, P = {}> = React.SFC<
35+
P & {
36+
render?: ChildrenFn<RP & P>
37+
}
38+
>
39+
40+
export type MapperValue<RP, P> = ReactElement<any> | MapperComponent<RP, P>
41+
42+
export type Mapper<RP, P> = Record<keyof RP, MapperValue<RP, P>>
43+
44+
export function adopt<RP, P = {}>(mapper: Mapper<RP, P>): RPC<RP, P> {
4245
if (!values(mapper).some(isValidRenderProp)) {
4346
throw new Error(
4447
'The render props object mapper just accept valid elements as value'
4548
)
4649
}
4750

48-
const Children: RPC<RP, P> = ({ children, ...rest }: any) =>
51+
const Children: any = ({ children, ...rest }: any) =>
4952
isFn(children) && children(rest)
5053

51-
const reducer = (Component: RPC<RP, P>, key: keyof RP): RPC<RP, P> => ({
54+
const reducer = (Component: RPC<RP>, key: string): RPC<RP> => ({
5255
children,
5356
...rest
54-
}: {
55-
children: ChildrenFn<RP>
5657
}) => (
5758
<Component {...rest}>
5859
{props => {
59-
const element: any = mapper[key]
60-
const propsWithoutRest = omit<RP>(props, keys(rest))
60+
const element = prop(key, mapper)
61+
const propsWithoutRest = omit<RP>(keys(rest), props)
6162

62-
const render = (cProps: Partial<RP>) =>
63+
const render: ChildrenFn<RP> = cProps =>
6364
isFn(children) &&
64-
children(assign({}, propsWithoutRest, { [key]: cProps }))
65+
children(
66+
assign({}, propsWithoutRest, {
67+
[key]: cProps,
68+
})
69+
)
6570

6671
return isFn(element)
6772
? React.createElement(element, assign({}, rest, props, { render }))

0 commit comments

Comments
 (0)