types(reactivity): ref type should not expose _isRef
This commit is contained in:
parent
68ad302714
commit
61d8941692
@ -69,7 +69,7 @@ export function computed<T>(
|
|||||||
set value(newValue: T) {
|
set value(newValue: T) {
|
||||||
setter(newValue)
|
setter(newValue)
|
||||||
}
|
}
|
||||||
}
|
} as any
|
||||||
}
|
}
|
||||||
|
|
||||||
function trackChildRun(childRunner: ReactiveEffect) {
|
function trackChildRun(childRunner: ReactiveEffect) {
|
||||||
|
@ -5,14 +5,28 @@ import { reactive, isReactive } from './reactive'
|
|||||||
import { ComputedRef } from './computed'
|
import { ComputedRef } from './computed'
|
||||||
import { CollectionTypes } from './collectionHandlers'
|
import { CollectionTypes } from './collectionHandlers'
|
||||||
|
|
||||||
|
const isRefSymbol = Symbol()
|
||||||
|
|
||||||
export interface Ref<T = any> {
|
export interface Ref<T = any> {
|
||||||
_isRef: true
|
// This field is necessary to allow TS to differentiate a Ref from a plain
|
||||||
|
// object that happens to have a "value" field.
|
||||||
|
// However, checking a symbol on an arbitrary object is much slower than
|
||||||
|
// checking a plain property, so we use a _isRef plain property for isRef()
|
||||||
|
// check in the actual implementation.
|
||||||
|
// The reason for not just declaring _isRef in the interface is because we
|
||||||
|
// don't want this internal field to leak into userland autocompletion -
|
||||||
|
// a private symbol, on the other hand, achieves just that.
|
||||||
|
[isRefSymbol]: true
|
||||||
value: UnwrapRef<T>
|
value: UnwrapRef<T>
|
||||||
}
|
}
|
||||||
|
|
||||||
const convert = <T extends unknown>(val: T): T =>
|
const convert = <T extends unknown>(val: T): T =>
|
||||||
isObject(val) ? reactive(val) : val
|
isObject(val) ? reactive(val) : val
|
||||||
|
|
||||||
|
export function isRef(r: any): r is Ref {
|
||||||
|
return r ? r._isRef === true : false
|
||||||
|
}
|
||||||
|
|
||||||
export function ref<T extends Ref>(raw: T): T
|
export function ref<T extends Ref>(raw: T): T
|
||||||
export function ref<T>(raw: T): Ref<T>
|
export function ref<T>(raw: T): Ref<T>
|
||||||
export function ref<T = any>(): Ref<T>
|
export function ref<T = any>(): Ref<T>
|
||||||
@ -37,11 +51,7 @@ export function ref(raw?: unknown) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return r as Ref
|
return r
|
||||||
}
|
|
||||||
|
|
||||||
export function isRef(r: any): r is Ref {
|
|
||||||
return r ? r._isRef === true : false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function toRefs<T extends object>(
|
export function toRefs<T extends object>(
|
||||||
@ -69,7 +79,7 @@ function toProxyRef<T extends object, K extends keyof T>(
|
|||||||
set value(newVal) {
|
set value(newVal) {
|
||||||
object[key] = newVal
|
object[key] = newVal
|
||||||
}
|
}
|
||||||
}
|
} as any
|
||||||
}
|
}
|
||||||
|
|
||||||
type UnwrapArray<T> = { [P in keyof T]: UnwrapRef<T[P]> }
|
type UnwrapArray<T> = { [P in keyof T]: UnwrapRef<T[P]> }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user