Skip to content

Commit 627b9df

Browse files
committedFeb 21, 2020
fix(types): improve ref typing, close #759
1 parent 33622d6 commit 627b9df

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed
 

‎packages/reactivity/src/ref.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ export interface Ref<T = any> {
2323
const convert = <T extends unknown>(val: T): T =>
2424
isObject(val) ? reactive(val) : val
2525

26-
export function isRef<T>(r: Ref<T> | T): r is Ref<T>
26+
export function isRef<T>(r: Ref<T> | unknown): r is Ref<T>
2727
export function isRef(r: any): r is Ref {
2828
return r ? r._isRef === true : false
2929
}
3030

31-
export function ref<T extends Ref>(value: T): T
31+
export function ref<T>(value: T): T extends Ref ? T : Ref<T>
3232
export function ref<T>(value: T): Ref<T>
3333
export function ref<T = any>(): Ref<T>
3434
export function ref(value?: unknown) {

‎test-dts/ref.test-d.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { expectType } from 'tsd'
2+
import { Ref, ref } from './index'
3+
import { isRef } from '@vue/reactivity'
4+
5+
function foo(arg: number | Ref<number>) {
6+
// ref coercing
7+
const coerced = ref(arg)
8+
expectType<Ref<number>>(coerced)
9+
10+
// isRef as type guard
11+
if (isRef(arg)) {
12+
expectType<Ref<number>>(arg)
13+
}
14+
}
15+
16+
foo(1)

0 commit comments

Comments
 (0)