Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types(runtime-core): fix type constraints for default in PropOptions #12227

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 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 @@ -440,8 +443,29 @@ describe('defineModel', () => {
},
})

// @ts-expect-error type / default mismatch
defineModel<string>({ default: 123 })
// default mismatch
{
// @ts-expect-error
defineModel<string>({ default: 123 })
// @ts-expect-error
defineModel<string>({ default: () => 123 })
// @ts-expect-error
defineModel<{ foo: number }>({ default: { foo: '' } })
// @ts-expect-error
defineModel<{ foo: number }>({
default: () => ({ foo: '' }),
})
// @ts-expect-error
defineModel<() => number>({
default: () => '',
})
// @ts-expect-error
// optional prop with default
defineModel<{ id?: number }>({
default: () => ({ foo: 'bar' }),
})
}

// @ts-expect-error unknown props option
defineModel({ foo: 123 })

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
8 changes: 7 additions & 1 deletion packages/runtime-core/src/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ type DefaultFactory<T> = (props: Data) => T | null | undefined
export interface PropOptions<T = any, D = T> {
type?: PropType<T> | true | null
required?: boolean
default?: D | DefaultFactory<D> | null | undefined | object
default?:
| null
| (T extends Function
? T extends infer F
? F
: never
: DefaultFactory<D> | D)
validator?(value: unknown, props: Data): boolean
/**
* @internal
Expand Down