From b8d3618afa273c8ecf267919d7db8c9bfc1eb0ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Thu, 30 Apr 2020 15:58:29 +0200 Subject: [PATCH] [IMP] hooks: improve useRef typings With this commit, we can get the type for the target component for a ref. By default, if the generic type is not given, the ref will simply use the base Component type. --- src/hooks.ts | 10 +++++----- tests/hooks.test.ts | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/hooks.ts b/src/hooks.ts index 3deaafbd..24d42a2d 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -94,12 +94,12 @@ export const onWillUpdateProps = makeAsyncHook("willUpdatePropsCB"); * The purpose of this hook is to allow components to get a reference to a sub * html node or component. */ -interface Ref { +interface Ref { el: HTMLElement | null; - comp: Component | null; + comp: C | null; } -export function useRef(name: string): Ref { +export function useRef(name: string): Ref { const __owl__ = Component.current!.__owl__; return { get el(): HTMLElement | null { @@ -111,9 +111,9 @@ export function useRef(name: string): Ref { } return null; }, - get comp(): Component | null { + get comp(): C | null { const val = __owl__.refs && __owl__.refs[name]; - return val instanceof Component ? val : null; + return val instanceof Component ? (val as C) : null; }, }; } diff --git a/tests/hooks.test.ts b/tests/hooks.test.ts index 2ef79a6d..2b594c27 100644 --- a/tests/hooks.test.ts +++ b/tests/hooks.test.ts @@ -267,7 +267,7 @@ describe("hooks", () => { class WidgetC extends Component { static template = xml`
Hello
`; static components = { WidgetB }; - ref = useRef("mywidgetb"); + ref = useRef("mywidgetb"); } const widget = new WidgetC();