From 375127e6937ed195635641f3d63dc92a43bf02b0 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Tue, 7 Nov 2023 12:03:47 -0500 Subject: [PATCH] transition anys --- __tests__/window.ts | 4 +++- src/index.tsx | 29 ++++++++++++++++++----------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/__tests__/window.ts b/__tests__/window.ts index 3eca6671..1ebc34a9 100644 --- a/__tests__/window.ts +++ b/__tests__/window.ts @@ -1,12 +1,14 @@ import { JSDOM } from 'jsdom'; +import type { DOMWindow } from 'jsdom'; const jsdom = new JSDOM('
', { url: 'https://nytimes.com', }); const { window } = jsdom; -function copyProps(src: any, target: any) { +function copyProps(src: DOMWindow, target: typeof globalThis) { const props = Object.getOwnPropertyNames(src) + // @ts-ignore .filter(prop => typeof target[prop] === 'undefined') .reduce( (result, prop) => ({ diff --git a/src/index.tsx b/src/index.tsx index 53a3e7ae..df2a1893 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -17,6 +17,8 @@ export * from './types'; export { default as HelmetData } from './HelmetData'; export { default as HelmetProvider } from './Provider'; +type Props = { [key: string]: any }; + export class Helmet extends Component> { static defaultProps = { defer: true, @@ -52,10 +54,10 @@ export class Helmet extends Component> { } flattenArrayTypeChildren( - child: any, - arrayTypeChildren: any, - newChildProps: any, - nestedChildren: any + child: JSX.Element, + arrayTypeChildren: { [key: string]: JSX.Element[] }, + newChildProps: Props, + nestedChildren: ReactNode ) { return { ...arrayTypeChildren, @@ -69,7 +71,12 @@ export class Helmet extends Component> { }; } - mapObjectTypeChildren(child: any, newProps: any, newChildProps: any, nestedChildren: any) { + mapObjectTypeChildren( + child: JSX.Element, + newProps: Props, + newChildProps: Props, + nestedChildren: ReactNode + ) { switch (child.type) { case TAG_NAMES.TITLE: return { @@ -97,7 +104,7 @@ export class Helmet extends Component> { } } - mapArrayTypeChildrenToProps(arrayTypeChildren: any, newProps: any) { + mapArrayTypeChildrenToProps(arrayTypeChildren: { [key: string]: JSX.Element }, newProps: Props) { let newFlattenedProps = { ...newProps }; Object.keys(arrayTypeChildren).forEach(arrayChildName => { @@ -110,7 +117,7 @@ export class Helmet extends Component> { return newFlattenedProps; } - warnOnInvalidChildren(child: any, nestedChildren: any) { + warnOnInvalidChildren(child: JSX.Element, nestedChildren: ReactNode) { invariant( VALID_TAG_NAMES.some(name => child.type === name), typeof child.type === 'function' @@ -133,24 +140,24 @@ export class Helmet extends Component> { return true; } - mapChildrenToProps(children: any, newProps: any) { + mapChildrenToProps(children: ReactNode, newProps: Props) { let arrayTypeChildren = {}; - React.Children.forEach(children, child => { + React.Children.forEach(children as JSX.Element, (child: ReactElement) => { if (!child || !child.props) { return; } const { children: nestedChildren, ...childProps } = child.props; // convert React props to HTML attributes - const newChildProps = Object.keys(childProps).reduce((obj: any, key) => { + const newChildProps = Object.keys(childProps).reduce((obj: Props, key) => { obj[HTML_TAG_MAP[key] || key] = childProps[key]; return obj; }, {}); let { type } = child; if (typeof type === 'symbol') { - type = type.toString(); + type = (type as 'symbol').toString(); } else { this.warnOnInvalidChildren(child, nestedChildren); }