Skip to content

Commit

Permalink
feat: infer the type of Props
Browse files Browse the repository at this point in the history
  • Loading branch information
odex21 committed Mar 15, 2023
1 parent 98b4010 commit 41c100f
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/CreateModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
TransitionProps,
Component,
InjectionKey,
// ConcreteComponent
Ref,
} from 'vue'
import { useRoute } from 'vue-router'
import { mergeDeepRight, mergeRight } from 'ramda'
Expand All @@ -26,8 +26,16 @@ export interface EventMap {
[x: string]: Handler
}

export interface OpenOptions<P> {
props?: P
export type MaybeRefOpions<T> = {
[P in keyof T]?: T[P] | Ref<T[P]>
}

export interface OpenOptions<C> {
props?: C extends Component<infer P>
? P extends { $props: infer Props }
? MaybeRefOpions<Omit<Props, 'modelValue'>>
: object
: object
on?: EventMap
zIndex?: number
}
Expand Down Expand Up @@ -167,7 +175,10 @@ export const createVfModal = <T extends ModalMap>(config: CreateConfig<T>) => {
/**
* open a modal with key
*/
const open = <K extends ModalKey>(key: K, opt?: OpenOptions<any>) => {
const open = <K extends ModalKey>(
key: K,
opt?: OpenOptions<T[K]['component']>
) => {
if (!modals[key]) {
throw new Error(`can not find the modal by key: ${key}`)
}
Expand Down Expand Up @@ -381,5 +392,3 @@ export const createVfModal = <T extends ModalMap>(config: CreateConfig<T>) => {
emitter,
}
}

// export type ComponentProps<C> = C extends ConcreteComponent<infer P> ? P : never

0 comments on commit 41c100f

Please sign in to comment.