Skip to content
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
10 changes: 7 additions & 3 deletions src/v3/reactivity/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@ declare const ShallowRefMarker: unique symbol

export type ShallowRef<T = any> = Ref<T> & { [ShallowRefMarker]?: true }

export function shallowRef<T>(value: T | Ref<T>): Ref<T> | ShallowRef<T>
export function shallowRef<T extends Ref>(value: T): T
export function shallowRef<T>(value: T): ShallowRef<T>
export function shallowRef<T>(
value: T
): Ref extends T
? T extends Ref
? IfAny<T, ShallowRef<T>, T>
: ShallowRef<T>
: ShallowRef<T>
export function shallowRef<T = any>(): ShallowRef<T | undefined>
export function shallowRef(value?: unknown) {
return createRef(value, true)
Expand Down
17 changes: 13 additions & 4 deletions types/test/v3/reactivity-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import {
markRaw,
shallowReadonly,
set,
del
del,
ShallowRef
} from '../../index'
import { IsUnion, describe, expectType } from '../utils'
import { IsUnion, describe, expectType, expectError } from '../utils'

function plainType(arg: number | Ref<number>) {
// ref coercing
Expand Down Expand Up @@ -163,6 +164,15 @@ if (shallowStatus.value === 'initial') {
shallowStatus.value = 'invalidating'
}

{
// should return ShallowRef<T> | Ref<T>, not ShallowRef<T | Ref<T>>
expectType<ShallowRef<{ name: string }> | Ref<{ name: string }>>(
shallowRef({} as { name: string } | Ref<{ name: string }>)
)
expectType<ShallowRef<number> | Ref<string[]> | ShallowRef<string>>(
shallowRef('' as Ref<string[]> | string | number)
)
}
const refStatus = ref<Status>('initial')
if (refStatus.value === 'initial') {
expectType<Ref<Status>>(shallowStatus)
Expand Down Expand Up @@ -386,7 +396,6 @@ describe('set/del', () => {
del([], 'fse', 123)
})


{
//#12978
type Steps = { step: '1' } | { step: '2' }
Expand All @@ -395,4 +404,4 @@ describe('set/del', () => {

expectType<IsUnion<typeof shallowUnionGenParam>>(false)
expectType<IsUnion<typeof shallowUnionAsCast>>(false)
}
}