Skip to content

Commit

Permalink
fix(types): improve ref typing, close #759
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Feb 21, 2020
1 parent 33622d6 commit 627b9df
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/reactivity/src/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ export interface Ref<T = any> {
const convert = <T extends unknown>(val: T): T =>
isObject(val) ? reactive(val) : val

export function isRef<T>(r: Ref<T> | T): r is Ref<T>
export function isRef<T>(r: Ref<T> | unknown): r is Ref<T>
export function isRef(r: any): r is Ref {
return r ? r._isRef === true : false
}

export function ref<T extends Ref>(value: T): T
export function ref<T>(value: T): T extends Ref ? T : Ref<T>
export function ref<T>(value: T): Ref<T>
export function ref<T = any>(): Ref<T>
export function ref(value?: unknown) {
Expand Down
16 changes: 16 additions & 0 deletions test-dts/ref.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { expectType } from 'tsd'
import { Ref, ref } from './index'
import { isRef } from '@vue/reactivity'

function foo(arg: number | Ref<number>) {
// ref coercing
const coerced = ref(arg)
expectType<Ref<number>>(coerced)

// isRef as type guard
if (isRef(arg)) {
expectType<Ref<number>>(arg)
}
}

foo(1)

0 comments on commit 627b9df

Please sign in to comment.