-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(types): ensure correct oldValue typing based on lazy option
close #719
- Loading branch information
Showing
3 changed files
with
88 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { ref, computed, watch } from './index' | ||
import { expectType } from 'tsd' | ||
|
||
const source = ref('foo') | ||
const source2 = computed(() => source.value) | ||
const source3 = () => 1 | ||
|
||
// eager watcher's oldValue will be undefined on first run. | ||
watch(source, (value, oldValue) => { | ||
expectType<string>(value) | ||
expectType<string | undefined>(oldValue) | ||
}) | ||
|
||
watch([source, source2, source3], (values, oldValues) => { | ||
expectType<(string | number)[]>(values) | ||
expectType<(string | number | undefined)[]>(oldValues) | ||
}) | ||
|
||
// const array | ||
watch([source, source2, source3] as const, (values, oldValues) => { | ||
expectType<Readonly<[string, string, number]>>(values) | ||
expectType< | ||
Readonly<[string | undefined, string | undefined, number | undefined]> | ||
>(oldValues) | ||
}) | ||
|
||
// lazy watcher will have consistent types for oldValue. | ||
watch( | ||
source, | ||
(value, oldValue) => { | ||
expectType<string>(value) | ||
expectType<string>(oldValue) | ||
}, | ||
{ lazy: true } | ||
) | ||
|
||
watch( | ||
[source, source2, source3], | ||
(values, oldValues) => { | ||
expectType<(string | number)[]>(values) | ||
expectType<(string | number)[]>(oldValues) | ||
}, | ||
{ lazy: true } | ||
) | ||
|
||
// const array | ||
watch( | ||
[source, source2, source3] as const, | ||
(values, oldValues) => { | ||
expectType<Readonly<[string, string, number]>>(values) | ||
expectType<Readonly<[string, string, number]>>(oldValues) | ||
}, | ||
{ lazy: true } | ||
) |