Skip to content

Commit b4e4655

Browse files
authored
types: improve typings for shallowRef (#457)
1 parent 918f835 commit b4e4655

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/reactivity/ref.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,10 @@ export function toRef<T extends object, K extends keyof T>(
167167
})
168168
}
169169

170-
export function shallowRef<T>(value: T): T extends Ref ? T : Ref<T>
170+
export function shallowRef<T extends object>(
171+
value: T
172+
): T extends Ref ? T : Ref<T>
173+
export function shallowRef<T>(value: T): Ref<T>
171174
export function shallowRef<T = any>(): Ref<T | undefined>
172175
export function shallowRef(raw?: unknown) {
173176
if (isRef(raw)) {

test-dts/ref.test-d.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import { Ref, ref, isRef, unref, reactive, expectType } from './index'
1+
import {
2+
Ref,
3+
ref,
4+
shallowRef,
5+
isRef,
6+
unref,
7+
reactive,
8+
expectType,
9+
} from './index'
210

311
function plainType(arg: number | Ref<number>) {
412
// ref coercing
@@ -92,3 +100,18 @@ const state = reactive({
92100
})
93101

94102
expectType<string>(state.foo.label)
103+
104+
type Status = 'initial' | 'ready' | 'invalidating'
105+
const shallowStatus = shallowRef<Status>('initial')
106+
if (shallowStatus.value === 'initial') {
107+
expectType<Ref<Status>>(shallowStatus)
108+
expectType<Status>(shallowStatus.value)
109+
shallowStatus.value = 'invalidating'
110+
}
111+
112+
const refStatus = ref<Status>('initial')
113+
if (refStatus.value === 'initial') {
114+
expectType<Ref<Status>>(shallowStatus)
115+
expectType<Status>(shallowStatus.value)
116+
refStatus.value = 'invalidating'
117+
}

0 commit comments

Comments
 (0)