2021-08-20 12:07:16 +08:00
|
|
|
import { WritableComputedRef } from '@vue/reactivity'
|
2021-09-05 04:42:39 +08:00
|
|
|
import { expectType, ref, computed, Ref, ComputedRef } from './index'
|
2021-08-24 08:35:12 +08:00
|
|
|
import 'vue/ref-macros'
|
2021-09-05 04:42:39 +08:00
|
|
|
import { RefType, RefTypes } from 'vue/ref-macros'
|
2021-08-24 08:35:12 +08:00
|
|
|
|
|
|
|
// wrapping refs
|
2021-09-05 04:42:39 +08:00
|
|
|
|
2021-08-24 08:35:12 +08:00
|
|
|
// normal
|
2021-09-05 04:42:39 +08:00
|
|
|
let n = $(ref(1))
|
|
|
|
n = 2
|
|
|
|
// @ts-expect-error
|
|
|
|
n = 'foo'
|
|
|
|
|
|
|
|
// #4499 nullable
|
|
|
|
let msg = $(ref<string | null>(null))
|
|
|
|
msg = 'hello world'
|
|
|
|
msg = null
|
|
|
|
expectType<RefTypes.Ref | undefined>(msg![RefType])
|
|
|
|
|
2021-08-24 08:35:12 +08:00
|
|
|
// computed
|
2021-09-05 04:42:39 +08:00
|
|
|
let m = $(computed(() => n + 1))
|
|
|
|
m * 1
|
|
|
|
// @ts-expect-error
|
|
|
|
m.slice()
|
|
|
|
expectType<RefTypes.ComputedRef | undefined>(m[RefType])
|
|
|
|
|
2021-08-24 08:35:12 +08:00
|
|
|
// writable computed
|
2021-09-05 04:42:39 +08:00
|
|
|
let wc = $(
|
|
|
|
computed({
|
|
|
|
get: () => n + 1,
|
|
|
|
set: v => (n = v - 1)
|
|
|
|
})
|
|
|
|
)
|
|
|
|
wc = 2
|
|
|
|
// @ts-expect-error
|
|
|
|
wc = 'foo'
|
|
|
|
expectType<RefTypes.WritableComputedRef | undefined>(wc[RefType])
|
2021-08-24 08:35:12 +08:00
|
|
|
|
|
|
|
// destructure
|
2021-09-05 04:42:39 +08:00
|
|
|
function useFoo() {
|
|
|
|
let x = $ref(1)
|
|
|
|
let y = $computed(() => 'hi')
|
|
|
|
|
|
|
|
return $$({
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
z: 123
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const fooRes = useFoo()
|
|
|
|
const { x, y, z } = $(fooRes)
|
2021-08-24 08:35:12 +08:00
|
|
|
expectType<number>(x)
|
|
|
|
expectType<string>(y)
|
|
|
|
expectType<number>(z)
|
2021-07-17 05:35:04 +08:00
|
|
|
|
|
|
|
// $ref
|
|
|
|
expectType<number>($ref(1))
|
|
|
|
expectType<number>($ref(ref(1)))
|
|
|
|
expectType<{ foo: number }>($ref({ foo: ref(1) }))
|
|
|
|
|
2021-08-11 22:19:58 +08:00
|
|
|
// $shallowRef
|
|
|
|
expectType<number>($shallowRef(1))
|
|
|
|
expectType<{ foo: Ref<number> }>($shallowRef({ foo: ref(1) }))
|
|
|
|
|
2021-07-17 05:35:04 +08:00
|
|
|
// $computed
|
|
|
|
expectType<number>($computed(() => 1))
|
|
|
|
let b = $ref(1)
|
2021-08-20 12:07:16 +08:00
|
|
|
expectType<number>(
|
|
|
|
$computed(() => b, {
|
|
|
|
onTrack() {}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
// writable computed
|
|
|
|
expectType<number>(
|
|
|
|
$computed({
|
|
|
|
get: () => 1,
|
|
|
|
set: () => {}
|
|
|
|
})
|
|
|
|
)
|
2021-07-17 05:35:04 +08:00
|
|
|
|
2021-08-24 08:35:12 +08:00
|
|
|
// $$
|
2021-09-05 04:42:39 +08:00
|
|
|
const xRef = $$(x)
|
|
|
|
expectType<Ref<number>>(xRef)
|
|
|
|
|
|
|
|
const yRef = $$(y)
|
|
|
|
expectType<ComputedRef<string>>(yRef)
|
2021-07-17 05:35:04 +08:00
|
|
|
|
|
|
|
const c = $computed(() => 1)
|
2021-08-24 08:35:12 +08:00
|
|
|
const cRef = $$(c)
|
2021-07-17 05:35:04 +08:00
|
|
|
expectType<ComputedRef<number>>(cRef)
|
2021-08-20 12:07:16 +08:00
|
|
|
|
|
|
|
const c2 = $computed({
|
|
|
|
get: () => 1,
|
|
|
|
set: () => {}
|
|
|
|
})
|
2021-08-24 08:35:12 +08:00
|
|
|
const c2Ref = $$(c2)
|
2021-08-20 12:07:16 +08:00
|
|
|
expectType<WritableComputedRef<number>>(c2Ref)
|
2021-08-24 08:35:12 +08:00
|
|
|
|
|
|
|
// $$ on object
|
2021-09-05 04:42:39 +08:00
|
|
|
const obj = $$({
|
|
|
|
n,
|
|
|
|
m,
|
|
|
|
wc
|
|
|
|
})
|
|
|
|
|
|
|
|
expectType<Ref<number>>(obj.n)
|
|
|
|
expectType<ComputedRef<number>>(obj.m)
|
|
|
|
expectType<WritableComputedRef<number>>(obj.wc)
|