]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
types: improve ref sugar $computed typing
authorEvan You <yyx990803@gmail.com>
Fri, 20 Aug 2021 04:07:16 +0000 (00:07 -0400)
committerEvan You <yyx990803@gmail.com>
Fri, 20 Aug 2021 04:07:16 +0000 (00:07 -0400)
packages/runtime-core/src/helpers/refSugar.ts
test-dts/refSugar.test-d.ts

index 91480dfa94f1a88b3d629e07e67f9f8799d77e05..b9d8d7c9b6722123714101161e4eee6130f354a9 100644 (file)
@@ -1,4 +1,12 @@
-import { Ref, UnwrapRef, ShallowUnwrapRef, ComputedRef } from '@vue/reactivity'
+import {
+  Ref,
+  UnwrapRef,
+  ShallowUnwrapRef,
+  ComputedRef,
+  WritableComputedOptions,
+  DebuggerOptions,
+  WritableComputedRef
+} from '@vue/reactivity'
 
 export function $ref<T>(arg: T | Ref<T>): UnwrapRef<T>
 export function $ref() {}
@@ -8,9 +16,19 @@ export function $shallowRef<T>(arg: T): T {
 }
 
 declare const ComputedRefMarker: unique symbol
-type ComputedRefValue<T> = T & { [ComputedRefMarker]?: any }
+type ComputedValue<T> = T & { [ComputedRefMarker]?: any }
 
-export function $computed<T>(getter: () => T): ComputedRefValue<T>
+declare const WritableComputedRefMarker: unique symbol
+type WritableComputedValue<T> = T & { [WritableComputedRefMarker]?: any }
+
+export function $computed<T>(
+  getter: () => T,
+  debuggerOptions?: DebuggerOptions
+): ComputedValue<T>
+export function $computed<T>(
+  options: WritableComputedOptions<T>,
+  debuggerOptions?: DebuggerOptions
+): WritableComputedValue<T>
 export function $computed() {}
 
 export function $fromRefs<T>(source: T): ShallowUnwrapRef<T>
@@ -18,7 +36,8 @@ export function $fromRefs() {
   return null as any
 }
 
-export function $raw<T>(value: ComputedRefValue<T>): ComputedRef<T>
+export function $raw<T>(value: ComputedValue<T>): ComputedRef<T>
+export function $raw<T>(value: WritableComputedValue<T>): WritableComputedRef<T>
 export function $raw<T>(value: T): Ref<T>
 export function $raw() {
   return null as any
index 4ea579b91cb96fbe38ce448a6e3e68e5e759906b..3f8c670d9eb85f1142ec51e11451f1b9d0036c82 100644 (file)
@@ -1,3 +1,4 @@
+import { WritableComputedRef } from '@vue/reactivity'
 import {
   expectType,
   $ref,
@@ -22,7 +23,19 @@ expectType<{ foo: Ref<number> }>($shallowRef({ foo: ref(1) }))
 // $computed
 expectType<number>($computed(() => 1))
 let b = $ref(1)
-expectType<number>($computed(() => b))
+expectType<number>(
+  $computed(() => b, {
+    onTrack() {}
+  })
+)
+
+// writable computed
+expectType<number>(
+  $computed({
+    get: () => 1,
+    set: () => {}
+  })
+)
 
 function useFoo() {
   return {
@@ -45,3 +58,10 @@ expectType<Ref<string>>($raw(y))
 const c = $computed(() => 1)
 const cRef = $raw(c)
 expectType<ComputedRef<number>>(cRef)
+
+const c2 = $computed({
+  get: () => 1,
+  set: () => {}
+})
+const c2Ref = $raw(c2)
+expectType<WritableComputedRef<number>>(c2Ref)