Skip to content

Commit

Permalink
fix: handle optional prop with default
Browse files Browse the repository at this point in the history
  • Loading branch information
jh-leong committed Oct 22, 2024
1 parent 15c6f92 commit 609d33b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 8 additions & 0 deletions packages-private/dts-test/setupHelpers.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,9 @@ describe('defineModel', () => {
const countDefault = defineModel<number>('count', { default: 1 })
expectType<Ref<number>>(countDefault)

const modelNull = defineModel<null>({ default: null })
expectType<Ref<null>>(modelNull)

// infer type from default
const inferred = defineModel({ default: 123 })
expectType<Ref<number | undefined>>(inferred)
Expand Down Expand Up @@ -456,6 +459,11 @@ describe('defineModel', () => {
defineModel<() => number>({
default: () => '',
})
// @ts-expect-error
// optional prop with default
defineModel<{ id?: number }>({
default: () => ({ foo: 'bar' }),
})
}

// @ts-expect-error unknown props option
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-core/src/apiSetupHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ export type DefineModelOptions<T = any, G = T, S = T> = {
* ```
*/
export function defineModel<T, M extends PropertyKey = string, G = T, S = T>(
options: ({ default: any } | { required: true }) &
options: ({ default: unknown } | { required: true }) &
PropOptions<T> &
DefineModelOptions<T, G, S>,
): ModelRef<T, M, G, S>
Expand All @@ -296,7 +296,7 @@ export function defineModel<T, M extends PropertyKey = string, G = T, S = T>(

export function defineModel<T, M extends PropertyKey = string, G = T, S = T>(
name: string,
options: ({ default: any } | { required: true }) &
options: ({ default: unknown } | { required: true }) &
PropOptions<T> &
DefineModelOptions<T, G, S>,
): ModelRef<T, M, G, S>
Expand Down

0 comments on commit 609d33b

Please sign in to comment.