Skip to content

Commit 636a861

Browse files
feat(types): add type TemplateRef (#12645)
* feat(types): add type TemplateRef * chore: simplify Co-authored-by: jh-leong <[email protected]> --------- Co-authored-by: jh-leong <[email protected]>
1 parent d48937f commit 636a861

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

packages-private/dts-test/ref.test-d.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
type MaybeRefOrGetter,
55
type Ref,
66
type ShallowRef,
7+
type TemplateRef,
78
type ToRefs,
89
type WritableComputedRef,
910
computed,
@@ -535,7 +536,7 @@ expectType<string>(toValue(unref2))
535536

536537
// useTemplateRef
537538
const tRef = useTemplateRef('foo')
538-
expectType<Readonly<ShallowRef<unknown>>>(tRef)
539+
expectType<TemplateRef>(tRef)
539540

540541
const tRef2 = useTemplateRef<HTMLElement>('bar')
541-
expectType<Readonly<ShallowRef<HTMLElement | null>>>(tRef2)
542+
expectType<TemplateRef<HTMLElement>>(tRef2)

packages/runtime-core/src/helpers/useTemplateRef.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import { EMPTY_OBJ } from '@vue/shared'
55

66
export const knownTemplateRefs: WeakSet<ShallowRef> = new WeakSet()
77

8+
export type TemplateRef<T = unknown> = Readonly<ShallowRef<T | null>>
9+
810
export function useTemplateRef<T = unknown, Keys extends string = string>(
911
key: Keys,
10-
): Readonly<ShallowRef<T | null>> {
12+
): TemplateRef<T> {
1113
const i = getCurrentInstance()
1214
const r = shallowRef(null)
1315
if (i) {

packages/runtime-core/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export { defineComponent } from './apiDefineComponent'
6464
export { defineAsyncComponent } from './apiAsyncComponent'
6565
export { useAttrs, useSlots } from './apiSetupHelpers'
6666
export { useModel } from './helpers/useModel'
67-
export { useTemplateRef } from './helpers/useTemplateRef'
67+
export { useTemplateRef, type TemplateRef } from './helpers/useTemplateRef'
6868
export { useId } from './helpers/useId'
6969
export {
7070
hydrateOnIdle,

0 commit comments

Comments
 (0)