Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): should wrap the top level setup refs with ShallowReactive #12272

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/reactivity/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export {
type Raw,
type DeepReadonly,
type ShallowReactive,
type IsShallowReactive,
type UnwrapNestedRefs,
type Reactive,
type ReactiveMarker,
Expand Down
4 changes: 4 additions & 0 deletions packages/reactivity/src/reactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ export declare const ShallowReactiveMarker: unique symbol

export type ShallowReactive<T> = T & { [ShallowReactiveMarker]?: true }

export type IsShallowReactive<T> = T extends { [ShallowReactiveMarker]?: true }
? true
: false

/**
* Shallow version of {@link reactive()}.
*
Expand Down
13 changes: 10 additions & 3 deletions packages/reactivity/src/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { Dep, getDepFromReactive } from './dep'
import {
type Builtin,
type ShallowReactive,
type ShallowReactiveMarker,
isProxy,
isReactive,
Expand Down Expand Up @@ -489,10 +490,16 @@ export type ShallowUnwrapRef<T> = {
[K in keyof T]: DistributeRef<T[K]>
}

type DistributeRef<T> = T extends Ref<infer V, unknown> ? V : T
type DistributeRef<T> =
T extends Ref<infer V, unknown>
? V extends object
? ShallowReactive<V>
: V
: T

export type UnwrapRef<T> =
T extends ShallowRef<infer V, unknown>
export type UnwrapRef<T> = T extends undefined
? T
: T extends ShallowRef<infer V, unknown>
? V
: T extends Ref<infer V, unknown>
? UnwrapRefSimple<V>
Expand Down
1 change: 1 addition & 0 deletions packages/runtime-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ export type {
ReactiveFlags,
DeepReadonly,
ShallowReactive,
IsShallowReactive,
UnwrapNestedRefs,
ComputedRef,
WritableComputedRef,
Expand Down