Skip to content

Commit

Permalink
fix: 修复v-model类型
Browse files Browse the repository at this point in the history
  • Loading branch information
agileago committed Dec 10, 2021
1 parent eda8293 commit 5d69c59
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
7 changes: 4 additions & 3 deletions example/example.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const focusDirective: Directive = {

interface Foo_Props {
size: 'small' | 'large'
modelValue?: string
'onUpdate:modelValue'?: (val: string) => void
// 组件的slots
slots: {
item(name: string): VNodeChild
Expand All @@ -17,9 +19,8 @@ interface Foo_Props {

class Foo extends VueComponent<Foo_Props> {
// vue需要的运行时属性检查
static defaultProps: ComponentProps<Foo_Props> = {
size: String,
}
static defaultProps: ComponentProps<Foo_Props> = ['size', 'modelValue', 'onUpdate:modelValue']

// 组件需要的局部指令
static directives: Record<string, Directive> = {
focus: focusDirective,
Expand Down
7 changes: 5 additions & 2 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@ type ModelProps<T extends {}> = Exclude<
undefined
>

export type WithVModel<T extends {}, U extends keyof T = ModelProps<T>> = {
export type WithVModel<T extends {}, U extends keyof T = ModelProps<T>> = TransformModelValue<{
[k in U as `v-model:${k & string}`]?: T[k] | [T[k], string[]]
}
}>
export type TransformModelValue<T extends {}> = 'v-model:modelValue' extends keyof T
? Omit<T, 'v-model:modelValue'> & { ['v-model']?: T['v-model:modelValue'] }
: T

export type ComponentProps<T extends {}> = ComponentPropsArray<T> | ComponentPropsObject<T>

Expand Down

0 comments on commit 5d69c59

Please sign in to comment.