Skip to content

Commit

Permalink
Improve types 📈
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlesMangwa committed Feb 13, 2021
1 parent a2296fc commit 10ce351
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 15 deletions.
3 changes: 1 addition & 2 deletions lib/ModalState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ const createModalState = (): ModalStateType<any> => {

setState<P>({
currentModal: modalName,
// @ts-ignore
stack: {
...state.stack,
openedItems: state.stack.openedItems.add(
Expand All @@ -119,7 +118,7 @@ const createModalState = (): ModalStateType<any> => {
...(params && { params }),
}),
),
},
} as ModalContextProvider<P>['stack'],
})
}

Expand Down
8 changes: 3 additions & 5 deletions lib/createModalStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,16 @@ export default function <P>(
openedItemsSize: 0,
}

// @ts-ignore
return Object.entries(config).reduce<ModalStack<P>>(
// @ts-ignore
(output, entry, index) => {
(output, entry, index): ModalStack<P> => {
const { name, component, options } = getStackItemData(entry[0], entry[1])
return {
...output,
names: [...output.names, name],
names: [...output.names, name] as ModalStack<P>['names'],
content: [
...output.content,
{ index, name, component, hash: '', ...(options && { options }) },
],
] as ModalStack<P>['content'],
}
},
initialStack,
Expand Down
3 changes: 1 addition & 2 deletions lib/useModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import ModalContext from './ModalContext'
* @see https://colorfy-software.gitbook.io/react-native-modalfy/api/usemodal
*/
export default function <P extends ModalfyParams>(): UsableModalProp<P> {
// @ts-ignore
const context: UsableModalProp<P> = useContext(ModalContext)
const context = useContext(ModalContext) as UsableModalProp<P>
return {
/**
* This function closes every open modal.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"peerDependencies": {
"react": ">=16.8.3",
"react-native": ">=0.59.0",
"use-memo-one": "*",
"react-native-gesture-handler": "*"
},
"keywords": [
Expand Down
11 changes: 5 additions & 6 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export interface ModalContextProvider<
paramName: N,
defaultValue?: D,
) => D extends P[M][N] ? P[M][N] : undefined
openModal: (modalName: M, params?: P[M]) => void
openModal: <N extends M>(modalName: N, params?: P[N]) => void
stack: ModalStack<P>
}

Expand Down Expand Up @@ -125,9 +125,9 @@ export type ModalState<P> = Omit<
ModalContextProvider<P>,
'currentModal' | 'stack' | 'openModal'
> & {
openModal: <M extends Exclude<keyof P, symbol | number>>(
modalName: M,
params?: P[M],
openModal: <M extends Exclude<keyof P, symbol | number>, N extends M>(
modalName: N,
params?: P[N],
isCalledOutsideOfContext?: boolean,
) => void
handleBackPress(): boolean
Expand Down Expand Up @@ -394,7 +394,7 @@ export type ModalProp<P extends ModalfyParams, Props = unknown> = Props & {
export type ModalComponentProp<
P extends ModalfyParams,
Props = unknown,
M = keyof P
M extends keyof P = keyof P
> = Props & {
/**
* Interface of the `modal` prop exposed by the library specifically to modal components.
Expand All @@ -403,7 +403,6 @@ export type ModalComponentProp<
*
* @see https://colorfy-software.gitbook.io/react-native-modalfy/guides/typing#modalcomponentprop
*/
// @ts-ignore
modal: UsableModalComponentProp<P, M>
}

Expand Down

0 comments on commit 10ce351

Please sign in to comment.