From 9a524d2225e76e6c0e32632f676f166717ac964b Mon Sep 17 00:00:00 2001 From: telamonian Date: Wed, 9 Oct 2019 18:53:00 -0400 Subject: [PATCH] added explicitly declared return type to `createIcon` also added explicit types to the call to `React.forwardRef` within `createIcon` --- packages/ui-components/src/icon/jlicon.tsx | 28 +++++++++++++++------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/packages/ui-components/src/icon/jlicon.tsx b/packages/ui-components/src/icon/jlicon.tsx index a207ff05bcb6..2ae0f793fd13 100644 --- a/packages/ui-components/src/icon/jlicon.tsx +++ b/packages/ui-components/src/icon/jlicon.tsx @@ -1,4 +1,9 @@ -import React from 'react'; +import React, { + ForwardRefExoticComponent, + PropsWithoutRef, + RefAttributes, + RefObject +} from 'react'; import { classes } from 'typestyle'; import { iconStyle, IIconStyle } from '../style/icon'; @@ -6,7 +11,7 @@ export function createIcon( svgname: string, svgstr: string, debug: boolean = false -) { +): JLIcon.IComponent { function resolveSvg(svgstr: string, title?: string): HTMLElement | null { const svgElement = new DOMParser().parseFromString(svgstr, 'image/svg+xml') .documentElement; @@ -33,8 +38,11 @@ export function createIcon( } } - const _JLIcon = React.forwardRef( - (props: JLIcon.IProps, ref: React.RefObject) => { + const JLIcon: JLIcon.IComponent = React.forwardRef< + HTMLDivElement, + JLIcon.IProps + >( + (props: JLIcon.IProps, ref: RefObject): JSX.Element => { const { className, title, tag = 'div', ...propsStyle } = props; const Tag = tag; const classNames = classes( @@ -59,11 +67,6 @@ export function createIcon( } ); - // widen type to include .element - let JLIcon: typeof _JLIcon & { - element: (props: JLIcon.IProps) => HTMLElement; - } = _JLIcon as any; - JLIcon.element = ({ className, title, @@ -114,6 +117,13 @@ export namespace JLIcon { */ title?: string; } + + export interface IComponent + extends ForwardRefExoticComponent< + PropsWithoutRef & RefAttributes + > { + element?: (props: IProps) => HTMLElement; + } } namespace Private {