From def27239bd18317a41a8914fb69baf51ca723fdb Mon Sep 17 00:00:00 2001 From: =?utf8?q?=E6=89=A9=E6=95=A3=E6=80=A7=E7=99=BE=E4=B8=87=E7=94=9C?= =?utf8?q?=E9=9D=A2=E5=8C=85?= Date: Thu, 10 Oct 2019 02:01:53 +0800 Subject: [PATCH] type: improve typing (#177) --- packages/reactivity/src/computed.ts | 3 +-- packages/reactivity/src/reactive.ts | 3 +-- packages/reactivity/src/ref.ts | 8 ++++---- packages/runtime-core/src/createRenderer.ts | 4 ++-- packages/runtime-core/src/h.ts | 2 +- 5 files changed, 9 insertions(+), 11 deletions(-) diff --git a/packages/reactivity/src/computed.ts b/packages/reactivity/src/computed.ts index 9f2ede3675..c1cf34cbb9 100644 --- a/packages/reactivity/src/computed.ts +++ b/packages/reactivity/src/computed.ts @@ -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 extends Ref { +export interface ComputedRef extends WritableComputedRef { readonly value: UnwrapNestedRefs - readonly effect: ReactiveEffect } export interface WritableComputedRef extends Ref { diff --git a/packages/reactivity/src/reactive.ts b/packages/reactivity/src/reactive.ts index b703154ee8..5fba512470 100644 --- a/packages/reactivity/src/reactive.ts +++ b/packages/reactivity/src/reactive.ts @@ -61,8 +61,7 @@ export function reactive(target: object) { export function readonly( target: T -): Readonly> -export function readonly(target: object) { +): Readonly> { // value is a mutable observable, retrieve its original and return // a readonly version. if (reactiveToRaw.has(target)) { diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index 113696bd45..9e000d979a 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -5,12 +5,12 @@ import { reactive } from './reactive' export const refSymbol = Symbol(__DEV__ ? 'refSymbol' : undefined) -export interface Ref { +export interface Ref { [refSymbol]: true value: UnwrapNestedRefs } -export type UnwrapNestedRefs = T extends Ref ? T : UnwrapRef +export type UnwrapNestedRefs = T extends Ref ? T : UnwrapRef const convert = (val: any): any => (isObject(val) ? reactive(val) : val) @@ -30,7 +30,7 @@ export function ref(raw: T): Ref { return v as Ref } -export function isRef(v: any): v is Ref { +export function isRef(v: any): v is Ref { return v ? v[refSymbol] === true : false } @@ -73,7 +73,7 @@ export type UnwrapRef = { array: T extends Array ? Array> : T object: { [K in keyof T]: UnwrapRef } stop: T -}[T extends Ref +}[T extends Ref ? 'ref' : T extends Array ? 'array' diff --git a/packages/runtime-core/src/createRenderer.ts b/packages/runtime-core/src/createRenderer.ts index 3a90d1e0f4..8211a4bdc7 100644 --- a/packages/runtime-core/src/createRenderer.ts +++ b/packages/runtime-core/src/createRenderer.ts @@ -1767,8 +1767,8 @@ export function createRenderer< } function setRef( - ref: string | Function | Ref, - oldRef: string | Function | Ref | null, + ref: string | Function | Ref, + oldRef: string | Function | Ref | null, parent: ComponentInternalInstance, value: HostNode | ComponentPublicInstance | null ) { diff --git a/packages/runtime-core/src/h.ts b/packages/runtime-core/src/h.ts index 3a29607a58..5b32f3b7c6 100644 --- a/packages/runtime-core/src/h.ts +++ b/packages/runtime-core/src/h.ts @@ -50,7 +50,7 @@ h('div', null, {}) export interface RawProps { [key: string]: any key?: string | number - ref?: string | Ref | Function + ref?: string | Ref | Function // used to differ from a single VNode object as children _isVNode?: never // used to differ from Array children -- 2.47.3