]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
feat(types): add type TemplateRef (#12645)
authorShinigami <chrissi92@hotmail.de>
Fri, 14 Mar 2025 00:17:49 +0000 (01:17 +0100)
committerGitHub <noreply@github.com>
Fri, 14 Mar 2025 00:17:49 +0000 (08:17 +0800)
* feat(types): add type TemplateRef

* chore: simplify

Co-authored-by: jh-leong <jh.leong@outlook.com>
---------

Co-authored-by: jh-leong <jh.leong@outlook.com>
packages-private/dts-test/ref.test-d.ts
packages/runtime-core/src/helpers/useTemplateRef.ts
packages/runtime-core/src/index.ts

index 89b80a70fb53e03678619c83c825ef38d0e8e4a8..cf99b7bca7a73493835d5edc4bd2d72f8a713e70 100644 (file)
@@ -4,6 +4,7 @@ import {
   type MaybeRefOrGetter,
   type Ref,
   type ShallowRef,
+  type TemplateRef,
   type ToRefs,
   type WritableComputedRef,
   computed,
@@ -535,7 +536,7 @@ expectType<string>(toValue(unref2))
 
 // useTemplateRef
 const tRef = useTemplateRef('foo')
-expectType<Readonly<ShallowRef<unknown>>>(tRef)
+expectType<TemplateRef>(tRef)
 
 const tRef2 = useTemplateRef<HTMLElement>('bar')
-expectType<Readonly<ShallowRef<HTMLElement | null>>>(tRef2)
+expectType<TemplateRef<HTMLElement>>(tRef2)
index 4cb10ea8139e462c9d9582c62f00531f45563240..f516d14c9bd8c229a398d9e0f0f1a924a8fff934 100644 (file)
@@ -5,9 +5,11 @@ import { EMPTY_OBJ } from '@vue/shared'
 
 export const knownTemplateRefs: WeakSet<ShallowRef> = new WeakSet()
 
+export type TemplateRef<T = unknown> = Readonly<ShallowRef<T | null>>
+
 export function useTemplateRef<T = unknown, Keys extends string = string>(
   key: Keys,
-): Readonly<ShallowRef<T | null>> {
+): TemplateRef<T> {
   const i = getCurrentInstance()
   const r = shallowRef(null)
   if (i) {
index 3871167b3eeaf4655d7fe7fd36781a1f3b560a87..9910f82102b06daa9f132f6e15dd86d908dccb66 100644 (file)
@@ -64,7 +64,7 @@ export { defineComponent } from './apiDefineComponent'
 export { defineAsyncComponent } from './apiAsyncComponent'
 export { useAttrs, useSlots } from './apiSetupHelpers'
 export { useModel } from './helpers/useModel'
-export { useTemplateRef } from './helpers/useTemplateRef'
+export { useTemplateRef, type TemplateRef } from './helpers/useTemplateRef'
 export { useId } from './helpers/useId'
 export {
   hydrateOnIdle,