Browse Source

fix(types): fix ref(false) type to Ref<boolean> (#1028)

Carlos Rodrigues 6 năm trước cách đây
mục cha
commit
0bdd889156
2 tập tin đã thay đổi với 14 bổ sung1 xóa
  1. 4 1
      packages/reactivity/src/ref.ts
  2. 10 0
      test-dts/ref.test-d.ts

+ 4 - 1
packages/reactivity/src/ref.ts

@@ -28,7 +28,10 @@ export function isRef(r: any): r is Ref {
   return r ? r._isRef === true : false
 }
 
-export function ref<T>(value: T): T extends Ref ? T : Ref<UnwrapRef<T>>
+export function ref<T extends object>(
+  value: T
+): T extends Ref ? T : Ref<UnwrapRef<T>>
+export function ref<T>(value: T): Ref<UnwrapRef<T>>
 export function ref<T = any>(): Ref<T | undefined>
 export function ref(value?: unknown) {
   return createRef(value)

+ 10 - 0
test-dts/ref.test-d.ts

@@ -21,6 +21,16 @@ function plainType(arg: number | Ref<number>) {
   expectType<Ref<{ foo: number }>>(nestedRef)
   expectType<{ foo: number }>(nestedRef.value)
 
+  // ref boolean
+  const falseRef = ref(false)
+  expectType<Ref<boolean>>(falseRef)
+  expectType<boolean>(falseRef.value)
+
+  // ref true
+  const trueRef = ref<true>(true)
+  expectType<Ref<true>>(trueRef)
+  expectType<true>(trueRef.value)
+
   // tuple
   expectType<[number, string]>(unref(ref([1, '1'])))