Skip to content

Commit

Permalink
types: improve typings for shallowRef (#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
pikax authored Aug 5, 2020
1 parent 918f835 commit b4e4655
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/reactivity/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ export function toRef<T extends object, K extends keyof T>(
})
}

export function shallowRef<T>(value: T): T extends Ref ? T : Ref<T>
export function shallowRef<T extends object>(
value: T
): T extends Ref ? T : Ref<T>
export function shallowRef<T>(value: T): Ref<T>
export function shallowRef<T = any>(): Ref<T | undefined>
export function shallowRef(raw?: unknown) {
if (isRef(raw)) {
Expand Down
25 changes: 24 additions & 1 deletion test-dts/ref.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { Ref, ref, isRef, unref, reactive, expectType } from './index'
import {
Ref,
ref,
shallowRef,
isRef,
unref,
reactive,
expectType,
} from './index'

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

expectType<string>(state.foo.label)

type Status = 'initial' | 'ready' | 'invalidating'
const shallowStatus = shallowRef<Status>('initial')
if (shallowStatus.value === 'initial') {
expectType<Ref<Status>>(shallowStatus)
expectType<Status>(shallowStatus.value)
shallowStatus.value = 'invalidating'
}

const refStatus = ref<Status>('initial')
if (refStatus.value === 'initial') {
expectType<Ref<Status>>(shallowStatus)
expectType<Status>(shallowStatus.value)
refStatus.value = 'invalidating'
}

0 comments on commit b4e4655

Please sign in to comment.