types(reactivity): add dts for proxyRefs & improve typings (#1786)
This commit is contained in:
parent
5002ab53d6
commit
44448fdd22
@ -1,4 +1,4 @@
|
|||||||
import { reactive, readonly, toRaw, ReactiveFlags } from './reactive'
|
import { reactive, readonly, toRaw, ReactiveFlags, Target } from './reactive'
|
||||||
import { TrackOpTypes, TriggerOpTypes } from './operations'
|
import { TrackOpTypes, TriggerOpTypes } from './operations'
|
||||||
import { track, trigger, ITERATE_KEY } from './effect'
|
import { track, trigger, ITERATE_KEY } from './effect'
|
||||||
import {
|
import {
|
||||||
@ -41,7 +41,7 @@ const arrayInstrumentations: Record<string, Function> = {}
|
|||||||
})
|
})
|
||||||
|
|
||||||
function createGetter(isReadonly = false, shallow = false) {
|
function createGetter(isReadonly = false, shallow = false) {
|
||||||
return function get(target: object, key: string | symbol, receiver: object) {
|
return function get(target: Target, key: string | symbol, receiver: object) {
|
||||||
if (key === ReactiveFlags.IS_REACTIVE) {
|
if (key === ReactiveFlags.IS_REACTIVE) {
|
||||||
return !isReadonly
|
return !isReadonly
|
||||||
} else if (key === ReactiveFlags.IS_READONLY) {
|
} else if (key === ReactiveFlags.IS_READONLY) {
|
||||||
@ -50,8 +50,8 @@ function createGetter(isReadonly = false, shallow = false) {
|
|||||||
key === ReactiveFlags.RAW &&
|
key === ReactiveFlags.RAW &&
|
||||||
receiver ===
|
receiver ===
|
||||||
(isReadonly
|
(isReadonly
|
||||||
? (target as any)[ReactiveFlags.READONLY]
|
? target[ReactiveFlags.READONLY]
|
||||||
: (target as any)[ReactiveFlags.REACTIVE])
|
: target[ReactiveFlags.REACTIVE])
|
||||||
) {
|
) {
|
||||||
return target
|
return target
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ export const enum ReactiveFlags {
|
|||||||
READONLY = '__v_readonly'
|
READONLY = '__v_readonly'
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Target {
|
export interface Target {
|
||||||
[ReactiveFlags.SKIP]?: boolean
|
[ReactiveFlags.SKIP]?: boolean
|
||||||
[ReactiveFlags.IS_REACTIVE]?: boolean
|
[ReactiveFlags.IS_REACTIVE]?: boolean
|
||||||
[ReactiveFlags.IS_READONLY]?: boolean
|
[ReactiveFlags.IS_READONLY]?: boolean
|
||||||
|
@ -1,4 +1,12 @@
|
|||||||
import { Ref, ref, isRef, unref, reactive, expectType } from './index'
|
import {
|
||||||
|
Ref,
|
||||||
|
ref,
|
||||||
|
isRef,
|
||||||
|
unref,
|
||||||
|
reactive,
|
||||||
|
expectType,
|
||||||
|
proxyRefs
|
||||||
|
} from './index'
|
||||||
|
|
||||||
function plainType(arg: number | Ref<number>) {
|
function plainType(arg: number | Ref<number>) {
|
||||||
// ref coercing
|
// ref coercing
|
||||||
@ -111,3 +119,21 @@ const state = reactive({
|
|||||||
})
|
})
|
||||||
|
|
||||||
expectType<string>(state.foo.label)
|
expectType<string>(state.foo.label)
|
||||||
|
|
||||||
|
// proxyRefs: should return `reactive` directly
|
||||||
|
const r1 = reactive({
|
||||||
|
k: 'v'
|
||||||
|
})
|
||||||
|
const p1 = proxyRefs(r1)
|
||||||
|
expectType<typeof r1>(p1)
|
||||||
|
|
||||||
|
// proxyRefs: `ShallowUnwrapRef`
|
||||||
|
const r2 = {
|
||||||
|
a: ref(1),
|
||||||
|
obj: {
|
||||||
|
k: ref('foo')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const p2 = proxyRefs(r2)
|
||||||
|
expectType<number>(p2.a)
|
||||||
|
expectType<Ref<string>>(p2.obj.k)
|
||||||
|
Loading…
Reference in New Issue
Block a user