expectType<Ref<string>>(p2.obj.k)
expectType<{ name: string } | null>(p2.union)
+const r3 = shallowReactive({
+ n: ref(1),
+})
+const p3 = proxyRefs(r3)
+expectType<Ref<number>>(p3.n)
+
// toRef and toRefs
{
const obj: {
expectType<number>(foo.value.a.b.value)
})
+describe('shallow reactive collection in reactive', () => {
+ const baz = reactive({
+ foo: shallowReactive(new Map([['a', ref(42)]])),
+ })
+
+ const foo = toRef(baz, 'foo')
+ expectType<Ref<number> | undefined>(foo.value.get('a'))
+})
+
describe('shallow ref in reactive', () => {
const x = reactive({
foo: shallowRef({
)
}
-export declare const ShallowReactiveMarker: unique symbol
+// Use a private class brand instead of a marker property so shallow-reactive
+// types remain distinguishable in `UnwrapRef` without leaking the brand into
+// `keyof`/indexed access types or requiring the property for plain assignment.
+declare class ShallowReactiveBrandClass {
+ private __shallowReactiveBrand?: never
+}
+
+export type ShallowReactiveBrand = ShallowReactiveBrandClass
-export type ShallowReactive<T> = T & { [ShallowReactiveMarker]: never }
+export type ShallowReactive<T> = T & ShallowReactiveBrand
/**
* Shallow version of {@link reactive}.
import { Dep, getDepFromReactive } from './dep'
import {
type Builtin,
- type ShallowReactiveMarker,
+ type ShallowReactiveBrand,
type Target,
isProxy,
isReactive,
*/
export interface RefUnwrapBailTypes {}
-export type ShallowUnwrapRef<T> = {
- [K in keyof T]: DistributeRef<T[K]>
-}
+export type ShallowUnwrapRef<T> = T extends ShallowReactiveBrand
+ ? T
+ : {
+ [K in keyof T]: DistributeRef<T[K]>
+ }
type DistributeRef<T> = T extends Ref<infer V, unknown> ? V : T
| RefUnwrapBailTypes[keyof RefUnwrapBailTypes]
| { [RawSymbol]?: true }
? T
- : T extends Map<infer K, infer V>
- ? Map<K, UnwrapRefSimple<V>> & UnwrapRef<Omit<T, keyof Map<any, any>>>
- : T extends WeakMap<infer K, infer V>
- ? WeakMap<K, UnwrapRefSimple<V>> &
- UnwrapRef<Omit<T, keyof WeakMap<any, any>>>
- : T extends Set<infer V>
- ? Set<UnwrapRefSimple<V>> & UnwrapRef<Omit<T, keyof Set<any>>>
- : T extends WeakSet<infer V>
- ? WeakSet<UnwrapRefSimple<V>> & UnwrapRef<Omit<T, keyof WeakSet<any>>>
- : T extends ReadonlyArray<any>
- ? { [K in keyof T]: UnwrapRefSimple<T[K]> }
- : T extends object & { [ShallowReactiveMarker]: never }
- ? T
+ : T extends ShallowReactiveBrand
+ ? T
+ : T extends Map<infer K, infer V>
+ ? Map<K, UnwrapRefSimple<V>> & UnwrapRef<Omit<T, keyof Map<any, any>>>
+ : T extends WeakMap<infer K, infer V>
+ ? WeakMap<K, UnwrapRefSimple<V>> &
+ UnwrapRef<Omit<T, keyof WeakMap<any, any>>>
+ : T extends Set<infer V>
+ ? Set<UnwrapRefSimple<V>> & UnwrapRef<Omit<T, keyof Set<any>>>
+ : T extends WeakSet<infer V>
+ ? WeakSet<UnwrapRefSimple<V>> &
+ UnwrapRef<Omit<T, keyof WeakSet<any>>>
+ : T extends ReadonlyArray<any>
+ ? { [K in keyof T]: UnwrapRefSimple<T[K]> }
: T extends object
? {
[P in keyof T]: P extends symbol ? T[P] : UnwrapRef<T[P]>