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

fix(types): fix defineModel watch type error #9942

Merged
merged 1 commit into from
Dec 30, 2023
Merged
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
37 changes: 36 additions & 1 deletion packages/dts-test/watch.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { computed, defineComponent, ref, shallowRef, watch } from 'vue'
import {
computed,
defineComponent,
defineModel,
ref,
shallowRef,
watch,
} from 'vue'
import { expectType } from './utils'

const source = ref('foo')
Expand Down Expand Up @@ -106,3 +113,31 @@ defineComponent({
expectType<Steps>(value)
})
}

{
// defineModel
const bool = defineModel({ default: false })
watch(bool, value => {
expectType<boolean>(value)
})

const bool1 = defineModel<boolean>()
watch(bool1, value => {
expectType<boolean | undefined>(value)
})

const msg = defineModel<string>({ required: true })
watch(msg, value => {
expectType<string>(value)
})

const arr = defineModel<string[]>({ required: true })
watch(arr, value => {
expectType<string[]>(value)
})

const obj = defineModel<{ foo: string }>({ required: true })
watch(obj, value => {
expectType<{ foo: string }>(value)
})
}
14 changes: 7 additions & 7 deletions packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ const INITIAL_WATCHER_VALUE = {}

type MultiWatchSources = (WatchSource<unknown> | object)[]

// overload: single source + cb
export function watch<T, Immediate extends Readonly<boolean> = false>(
source: WatchSource<T>,
cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
options?: WatchOptions<Immediate>,
): WatchStopHandle

// overload: array of multiple sources + cb
export function watch<
T extends MultiWatchSources,
Expand All @@ -137,13 +144,6 @@ export function watch<
options?: WatchOptions<Immediate>,
): WatchStopHandle

// overload: single source + cb
export function watch<T, Immediate extends Readonly<boolean> = false>(
source: WatchSource<T>,
cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
options?: WatchOptions<Immediate>,
): WatchStopHandle

// overload: watching reactive object w/ cb
export function watch<
T extends object,
Expand Down