type: improve typing (#177)
This commit is contained in:
parent
8f1475b8dd
commit
def27239bd
@ -2,9 +2,8 @@ import { effect, ReactiveEffect, activeReactiveEffectStack } from './effect'
|
|||||||
import { Ref, refSymbol, UnwrapNestedRefs } from './ref'
|
import { Ref, refSymbol, UnwrapNestedRefs } from './ref'
|
||||||
import { isFunction, NOOP } from '@vue/shared'
|
import { isFunction, NOOP } from '@vue/shared'
|
||||||
|
|
||||||
export interface ComputedRef<T> extends Ref<T> {
|
export interface ComputedRef<T> extends WritableComputedRef<T> {
|
||||||
readonly value: UnwrapNestedRefs<T>
|
readonly value: UnwrapNestedRefs<T>
|
||||||
readonly effect: ReactiveEffect
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface WritableComputedRef<T> extends Ref<T> {
|
export interface WritableComputedRef<T> extends Ref<T> {
|
||||||
|
@ -61,8 +61,7 @@ export function reactive(target: object) {
|
|||||||
|
|
||||||
export function readonly<T extends object>(
|
export function readonly<T extends object>(
|
||||||
target: T
|
target: T
|
||||||
): Readonly<UnwrapNestedRefs<T>>
|
): Readonly<UnwrapNestedRefs<T>> {
|
||||||
export function readonly(target: object) {
|
|
||||||
// value is a mutable observable, retrieve its original and return
|
// value is a mutable observable, retrieve its original and return
|
||||||
// a readonly version.
|
// a readonly version.
|
||||||
if (reactiveToRaw.has(target)) {
|
if (reactiveToRaw.has(target)) {
|
||||||
|
@ -5,12 +5,12 @@ import { reactive } from './reactive'
|
|||||||
|
|
||||||
export const refSymbol = Symbol(__DEV__ ? 'refSymbol' : undefined)
|
export const refSymbol = Symbol(__DEV__ ? 'refSymbol' : undefined)
|
||||||
|
|
||||||
export interface Ref<T> {
|
export interface Ref<T = any> {
|
||||||
[refSymbol]: true
|
[refSymbol]: true
|
||||||
value: UnwrapNestedRefs<T>
|
value: UnwrapNestedRefs<T>
|
||||||
}
|
}
|
||||||
|
|
||||||
export type UnwrapNestedRefs<T> = T extends Ref<any> ? T : UnwrapRef<T>
|
export type UnwrapNestedRefs<T> = T extends Ref ? T : UnwrapRef<T>
|
||||||
|
|
||||||
const convert = (val: any): any => (isObject(val) ? reactive(val) : val)
|
const convert = (val: any): any => (isObject(val) ? reactive(val) : val)
|
||||||
|
|
||||||
@ -30,7 +30,7 @@ export function ref<T>(raw: T): Ref<T> {
|
|||||||
return v as Ref<T>
|
return v as Ref<T>
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isRef(v: any): v is Ref<any> {
|
export function isRef(v: any): v is Ref {
|
||||||
return v ? v[refSymbol] === true : false
|
return v ? v[refSymbol] === true : false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,7 +73,7 @@ export type UnwrapRef<T> = {
|
|||||||
array: T extends Array<infer V> ? Array<UnwrapRef<V>> : T
|
array: T extends Array<infer V> ? Array<UnwrapRef<V>> : T
|
||||||
object: { [K in keyof T]: UnwrapRef<T[K]> }
|
object: { [K in keyof T]: UnwrapRef<T[K]> }
|
||||||
stop: T
|
stop: T
|
||||||
}[T extends Ref<any>
|
}[T extends Ref
|
||||||
? 'ref'
|
? 'ref'
|
||||||
: T extends Array<any>
|
: T extends Array<any>
|
||||||
? 'array'
|
? 'array'
|
||||||
|
@ -1767,8 +1767,8 @@ export function createRenderer<
|
|||||||
}
|
}
|
||||||
|
|
||||||
function setRef(
|
function setRef(
|
||||||
ref: string | Function | Ref<any>,
|
ref: string | Function | Ref,
|
||||||
oldRef: string | Function | Ref<any> | null,
|
oldRef: string | Function | Ref | null,
|
||||||
parent: ComponentInternalInstance,
|
parent: ComponentInternalInstance,
|
||||||
value: HostNode | ComponentPublicInstance | null
|
value: HostNode | ComponentPublicInstance | null
|
||||||
) {
|
) {
|
||||||
|
@ -50,7 +50,7 @@ h('div', null, {})
|
|||||||
export interface RawProps {
|
export interface RawProps {
|
||||||
[key: string]: any
|
[key: string]: any
|
||||||
key?: string | number
|
key?: string | number
|
||||||
ref?: string | Ref<any> | Function
|
ref?: string | Ref | Function
|
||||||
// used to differ from a single VNode object as children
|
// used to differ from a single VNode object as children
|
||||||
_isVNode?: never
|
_isVNode?: never
|
||||||
// used to differ from Array children
|
// used to differ from Array children
|
||||||
|
Loading…
Reference in New Issue
Block a user