Skip to content

Commit

Permalink
refactor(apiWatch): remove redundant overload for readonly
Browse files Browse the repository at this point in the history
  • Loading branch information
jh-leong committed May 29, 2024
1 parent 088914b commit 1d90f9b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
25 changes: 24 additions & 1 deletion packages/dts-test/watch.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ import { expectType } from './utils'
const source = ref('foo')
const source2 = computed(() => source.value)
const source3 = () => 1
type Bar = Ref<string> | ComputedRef<string> | (() => number)

type Bar = Ref<string> | ComputedRef<string> | (() => number)
type Foo = readonly [Ref<string>, ComputedRef<string>, () => number]

const readonlyArr: Foo = [source, source2, source3]

// lazy watcher will have consistent types for oldValue.
watch(source, (value, oldValue) => {
expectType<string>(value)
Expand All @@ -44,6 +47,13 @@ watch(reactive([source, source2, source3]), value => {
watch(reactive([source, source2, source3] as const), value => {
expectType<Reactive<Foo>>(value)
})

// readonly array
watch(readonlyArr, (values, oldValues) => {
expectType<Readonly<[string, string, number]>>(values)
expectType<Readonly<[string, string, number]>>(oldValues)
})

// immediate watcher's oldValue will be undefined on first run.
watch(
source,
Expand Down Expand Up @@ -92,6 +102,19 @@ watch(reactive([source, source2, source3] as const), (value, oldVals) => {
expectType<Reactive<Foo>>(value)
expectType<Reactive<Foo> | undefined>(oldVals)
})

// readonly array
watch(
readonlyArr,
(values, oldValues) => {
expectType<Readonly<[string, string, number]>>(values)
expectType<
Readonly<[string | undefined, string | undefined, number | undefined]>
>(oldValues)
},
{ immediate: true },
)

// should provide correct ref.value inner type to callbacks
const nestedRefSource = ref({
foo: ref(1),
Expand Down
12 changes: 0 additions & 12 deletions packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,6 @@ export function watch<
options?: WatchOptions<Immediate>,
): WatchStopHandle

// overload: multiple sources w/ `as const`
// watch([foo, bar] as const, () => {})
// somehow [...T] breaks when the type is readonly
export function watch<
T extends Readonly<MultiWatchSources>,
Immediate extends Readonly<boolean> = false,
>(
source: T,
cb: WatchCallback<MapSources<T, false>, MapSources<T, Immediate>>,
options?: WatchOptions<Immediate>,
): WatchStopHandle

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

0 comments on commit 1d90f9b

Please sign in to comment.