type: improve typing (#177)

This commit is contained in:
扩散性百万甜面包 2019-10-10 02:01:53 +08:00 committed by Evan You
parent 8f1475b8dd
commit def27239bd
5 changed files with 9 additions and 11 deletions

View File

@ -2,9 +2,8 @@ import { effect, ReactiveEffect, activeReactiveEffectStack } from './effect'
import { Ref, refSymbol, UnwrapNestedRefs } from './ref'
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 effect: ReactiveEffect
}
export interface WritableComputedRef<T> extends Ref<T> {

View File

@ -61,8 +61,7 @@ export function reactive(target: object) {
export function readonly<T extends object>(
target: T
): Readonly<UnwrapNestedRefs<T>>
export function readonly(target: object) {
): Readonly<UnwrapNestedRefs<T>> {
// value is a mutable observable, retrieve its original and return
// a readonly version.
if (reactiveToRaw.has(target)) {

View File

@ -5,12 +5,12 @@ import { reactive } from './reactive'
export const refSymbol = Symbol(__DEV__ ? 'refSymbol' : undefined)
export interface Ref<T> {
export interface Ref<T = any> {
[refSymbol]: true
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)
@ -30,7 +30,7 @@ export function ref<T>(raw: T): 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
}
@ -73,7 +73,7 @@ export type UnwrapRef<T> = {
array: T extends Array<infer V> ? Array<UnwrapRef<V>> : T
object: { [K in keyof T]: UnwrapRef<T[K]> }
stop: T
}[T extends Ref<any>
}[T extends Ref
? 'ref'
: T extends Array<any>
? 'array'

View File

@ -1767,8 +1767,8 @@ export function createRenderer<
}
function setRef(
ref: string | Function | Ref<any>,
oldRef: string | Function | Ref<any> | null,
ref: string | Function | Ref,
oldRef: string | Function | Ref | null,
parent: ComponentInternalInstance,
value: HostNode | ComponentPublicInstance | null
) {

View File

@ -50,7 +50,7 @@ h('div', null, {})
export interface RawProps {
[key: string]: any
key?: string | number
ref?: string | Ref<any> | Function
ref?: string | Ref | Function
// used to differ from a single VNode object as children
_isVNode?: never
// used to differ from Array children