types(reactivity): adjust type exports (#4407)

This commit is contained in:
zhangenming 2021-09-02 04:49:12 +08:00 committed by GitHub
parent 4502a0eab5
commit a6e6253319
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 12 deletions

View File

@ -9,6 +9,7 @@ export {
customRef, customRef,
triggerRef, triggerRef,
Ref, Ref,
ToRef,
ToRefs, ToRefs,
UnwrapRef, UnwrapRef,
ShallowUnwrapRef, ShallowUnwrapRef,
@ -51,7 +52,8 @@ export {
ReactiveEffectOptions, ReactiveEffectOptions,
EffectScheduler, EffectScheduler,
DebuggerOptions, DebuggerOptions,
DebuggerEvent DebuggerEvent,
DebuggerEventExtraInfo
} from './effect' } from './effect'
export { export {
effectScope, effectScope,

View File

@ -5,7 +5,7 @@ import { reactive, isProxy, toRaw, isReactive } from './reactive'
import { CollectionTypes } from './collectionHandlers' import { CollectionTypes } from './collectionHandlers'
import { createDep, Dep } from './dep' import { createDep, Dep } from './dep'
export declare const RefSymbol: unique symbol declare const RefSymbol: unique symbol
export interface Ref<T = any> { export interface Ref<T = any> {
value: T value: T
@ -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 => const convert = <T extends unknown>(val: T): T =>
isObject(val) ? reactive(val) : val isObject(val) ? reactive(val) : val
@ -154,7 +147,7 @@ export function proxyRefs<T extends object>(
: new Proxy(objectWithRefs, shallowUnwrapHandlers) : new Proxy(objectWithRefs, shallowUnwrapHandlers)
} }
export type CustomRefFactory<T> = ( type CustomRefFactory<T> = (
track: () => void, track: () => void,
trigger: () => void trigger: () => void
) => { ) => {
@ -192,6 +185,11 @@ export function customRef<T>(factory: CustomRefFactory<T>): Ref<T> {
return new CustomRefImpl(factory) as any 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> { export function toRefs<T extends object>(object: T): ToRefs<T> {
if (__DEV__ && !isProxy(object)) { if (__DEV__ && !isProxy(object)) {
console.warn(`toRefs() expects a reactive object but received a plain one.`) console.warn(`toRefs() expects a reactive object but received a plain one.`)
@ -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>( export function toRef<T extends object, K extends keyof T>(
object: T, object: T,
key: K key: K

View File

@ -147,18 +147,19 @@ declare module '@vue/reactivity' {
} }
export { export {
Ref,
ToRef,
ToRefs,
ReactiveEffectOptions, ReactiveEffectOptions,
DebuggerEvent, DebuggerEvent,
DebuggerOptions, DebuggerOptions,
TrackOpTypes, TrackOpTypes,
TriggerOpTypes, TriggerOpTypes,
Ref,
ComputedRef, ComputedRef,
WritableComputedRef, WritableComputedRef,
UnwrapRef, UnwrapRef,
ShallowUnwrapRef, ShallowUnwrapRef,
WritableComputedOptions, WritableComputedOptions,
ToRefs,
DeepReadonly DeepReadonly
} from '@vue/reactivity' } from '@vue/reactivity'
export { export {