Skip to content

Commit

Permalink
feat(types): add type TemplateRef
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Jan 3, 2025
1 parent e8e8422 commit 3f7e6ca
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
5 changes: 3 additions & 2 deletions packages-private/dts-test/ref.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
type MaybeRefOrGetter,
type Ref,
type ShallowRef,
type TemplateRef,
type ToRefs,
type WritableComputedRef,
computed,
Expand Down Expand Up @@ -535,7 +536,7 @@ expectType<string>(toValue(unref2))

// useTemplateRef
const tRef = useTemplateRef('foo')
expectType<Readonly<ShallowRef<unknown>>>(tRef)
expectType<TemplateRef<unknown>>(tRef)

const tRef2 = useTemplateRef<HTMLElement>('bar')
expectType<Readonly<ShallowRef<HTMLElement | null>>>(tRef2)
expectType<TemplateRef<HTMLElement | null>>(tRef2)
4 changes: 3 additions & 1 deletion packages/runtime-core/src/helpers/useTemplateRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 | null> {
const i = getCurrentInstance()
const r = shallowRef(null)
if (i) {
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 3f7e6ca

Please sign in to comment.