Skip to content

Commit

Permalink
types(reactivity): adjust type exports (#4407)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangenming committed Sep 1, 2021
1 parent 4502a0e commit a6e6253
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
4 changes: 3 additions & 1 deletion packages/reactivity/src/index.ts
Expand Up @@ -9,6 +9,7 @@ export {
customRef,
triggerRef,
Ref,
ToRef,
ToRefs,
UnwrapRef,
ShallowUnwrapRef,
Expand Down Expand Up @@ -51,7 +52,8 @@ export {
ReactiveEffectOptions,
EffectScheduler,
DebuggerOptions,
DebuggerEvent
DebuggerEvent,
DebuggerEventExtraInfo
} from './effect'
export {
effectScope,
Expand Down
17 changes: 8 additions & 9 deletions packages/reactivity/src/ref.ts
Expand Up @@ -5,7 +5,7 @@ import { reactive, isProxy, toRaw, isReactive } from './reactive'
import { CollectionTypes } from './collectionHandlers'
import { createDep, Dep } from './dep'

export declare const RefSymbol: unique symbol
declare const RefSymbol: unique symbol

export interface Ref<T = any> {
value: T
Expand Down Expand Up @@ -60,13 +60,6 @@ export function triggerRefValue(ref: RefBase<any>, newVal?: any) {
}
}

export type ToRef<T> = [T] extends [Ref] ? T : Ref<UnwrapRef<T>>
export type ToRefs<T = any> = {
// #2687: somehow using ToRef<T[K]> here turns the resulting type into
// a union of multiple Ref<*> types instead of a single Ref<* | *> type.
[K in keyof T]: T[K] extends Ref ? T[K] : Ref<UnwrapRef<T[K]>>
}

const convert = <T extends unknown>(val: T): T =>
isObject(val) ? reactive(val) : val

Expand Down Expand Up @@ -154,7 +147,7 @@ export function proxyRefs<T extends object>(
: new Proxy(objectWithRefs, shallowUnwrapHandlers)
}

export type CustomRefFactory<T> = (
type CustomRefFactory<T> = (
track: () => void,
trigger: () => void
) => {
Expand Down Expand Up @@ -192,6 +185,11 @@ export function customRef<T>(factory: CustomRefFactory<T>): Ref<T> {
return new CustomRefImpl(factory) as any
}

export type ToRefs<T = any> = {
// #2687: somehow using ToRef<T[K]> here turns the resulting type into
// a union of multiple Ref<*> types instead of a single Ref<* | *> type.
[K in keyof T]: T[K] extends Ref ? T[K] : Ref<UnwrapRef<T[K]>>
}
export function toRefs<T extends object>(object: T): ToRefs<T> {
if (__DEV__ && !isProxy(object)) {
console.warn(`toRefs() expects a reactive object but received a plain one.`)
Expand All @@ -217,6 +215,7 @@ class ObjectRefImpl<T extends object, K extends keyof T> {
}
}

export type ToRef<T> = [T] extends [Ref] ? T : Ref<UnwrapRef<T>>
export function toRef<T extends object, K extends keyof T>(
object: T,
key: K
Expand Down
5 changes: 3 additions & 2 deletions packages/runtime-core/src/index.ts
Expand Up @@ -147,18 +147,19 @@ declare module '@vue/reactivity' {
}

export {
Ref,
ToRef,
ToRefs,
ReactiveEffectOptions,
DebuggerEvent,
DebuggerOptions,
TrackOpTypes,
TriggerOpTypes,
Ref,
ComputedRef,
WritableComputedRef,
UnwrapRef,
ShallowUnwrapRef,
WritableComputedOptions,
ToRefs,
DeepReadonly
} from '@vue/reactivity'
export {
Expand Down

0 comments on commit a6e6253

Please sign in to comment.