From 5a87c8f74ea81631e31b90a07c0eb2925e063cfb Mon Sep 17 00:00:00 2001 From: Jannick Garthen Date: Sun, 18 Aug 2019 18:37:28 +0200 Subject: [PATCH] fix(*): use unknown instead of any type for layout snapshots --- examples/index.tsx | 4 ++-- lib/ObserveViewport.tsx | 6 +++--- lib/ViewportProvider.tsx | 14 +++++++------- lib/hooks.ts | 12 +++++------- lib/modules.d.ts | 2 +- lib/types.ts | 2 +- lib/utils.ts | 5 ++--- 7 files changed, 21 insertions(+), 24 deletions(-) diff --git a/examples/index.tsx b/examples/index.tsx index e9dae55..934146b 100644 --- a/examples/index.tsx +++ b/examples/index.tsx @@ -181,5 +181,5 @@ setInterval(() => { , document.getElementById('root'), - ) -}, 1000) \ No newline at end of file + ); +}, 1000); diff --git a/lib/ObserveViewport.tsx b/lib/ObserveViewport.tsx index 36fccd1..13fc581 100644 --- a/lib/ObserveViewport.tsx +++ b/lib/ObserveViewport.tsx @@ -28,8 +28,8 @@ interface IState extends IChildProps {} interface IProps { children?: (props: IChildProps) => React.ReactNode; - onUpdate?: (props: IChildProps, layoutSnapshot: any) => void; - recalculateLayoutBeforeUpdate?: (props: IChildProps) => any; + onUpdate?: (props: IChildProps, layoutSnapshot: unknown) => void; + recalculateLayoutBeforeUpdate?: (props: IChildProps) => unknown; disableScrollUpdates: boolean; disableDimensionsUpdates: boolean; deferUpdateUntilIdle: boolean; @@ -98,7 +98,7 @@ export default class ObserveViewport extends React.Component { cancelAnimationFrame(this.tickId); } - handleViewportUpdate = (viewport: IViewport, layoutSnapshot: any) => { + handleViewportUpdate = (viewport: IViewport, layoutSnapshot: unknown) => { const scroll = this.props.disableScrollUpdates ? null : viewport.scroll; const dimensions = this.props.disableDimensionsUpdates ? null diff --git a/lib/ViewportProvider.tsx b/lib/ViewportProvider.tsx index d22ee5e..d17f13a 100644 --- a/lib/ViewportProvider.tsx +++ b/lib/ViewportProvider.tsx @@ -30,18 +30,18 @@ const getCurrentDefaultViewport = (() => { defaultValue = { scroll: getClientScroll(), dimensions: getClientDimensions(), - } + }; } - return defaultValue - } + return defaultValue; + }; })(); export const ViewportContext = React.createContext({ - removeViewportChangeListener: (handler: TViewportChangeHandler) => { }, + removeViewportChangeListener: (handler: TViewportChangeHandler) => {}, addViewportChangeListener: ( handler: TViewportChangeHandler, options: IViewportChangeOptions, - ) => { }, + ) => {}, getCurrentViewport: getCurrentDefaultViewport, hasRootProviderAsParent: false, version: '__VERSION__', @@ -84,7 +84,7 @@ const shouldSkipIteration = ( export default class ViewportProvider extends React.PureComponent< IProps, { hasListeners: boolean } - > { +> { static defaultProps: { experimentalSchedulerEnabled: false; }; @@ -145,7 +145,7 @@ export default class ViewportProvider extends React.PureComponent< if (recalculateLayoutBeforeUpdate) { const getDuration = createPerformanceMarker(); const layoutState = recalculateLayoutBeforeUpdate(state); - return [layoutState, getDuration()]; + return [layoutState, getDuration()] as const; } return null; }, diff --git a/lib/hooks.ts b/lib/hooks.ts index 44c82f4..850a792 100644 --- a/lib/hooks.ts +++ b/lib/hooks.ts @@ -21,10 +21,8 @@ interface IEffectOptions extends IOptions { recalculateLayoutBeforeUpdate?: (viewport: IViewport) => T; } -type HandleViewportChangeType = (viewport: IViewport, snapshot: any) => void; - -export const useViewportEffect = ( - handleViewportChange: HandleViewportChangeType, +export const useViewportEffect = ( + handleViewportChange: (viewport: IViewport, snapshot: T) => void, options: IViewPortEffectOptions = {}, ) => { const { @@ -58,7 +56,7 @@ export const useViewport = (options: IFullOptions = {}): IViewport => { return state; }; -export const useScrollEffect = ( +export const useScrollEffect = ( effect: (scroll: IScroll, snapshot: T) => void, options: IEffectOptions = {}, ) => { @@ -80,7 +78,7 @@ export const useScroll = (options: IOptions = {}): IScroll => { return scroll; }; -export const useDimensionsEffect = ( +export const useDimensionsEffect = ( effect: (scroll: IDimensions, snapshot: T) => void, options: IEffectOptions = {}, ) => { @@ -124,7 +122,7 @@ export const useRect = ( ); }; -export const useLayoutSnapshot = ( +export const useLayoutSnapshot = ( recalculateLayoutBeforeUpdate: (viewport: IViewport) => T, options: IFullOptions = {}, ): null | T => { diff --git a/lib/modules.d.ts b/lib/modules.d.ts index 66fd09b..913dee5 100644 --- a/lib/modules.d.ts +++ b/lib/modules.d.ts @@ -66,4 +66,4 @@ interface DOMRectReadOnly { toJSON: () => any; } -declare module "@testing-library/react" +declare module '@testing-library/react'; diff --git a/lib/types.ts b/lib/types.ts index 3ec98f7..0acef74 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -46,7 +46,7 @@ export interface IViewportChangeOptions { notifyDimensions: () => boolean; notifyOnlyWhenIdle: () => boolean; priority: () => PriorityType; - recalculateLayoutBeforeUpdate?: (viewport: IViewport) => any; + recalculateLayoutBeforeUpdate?: (viewport: IViewport) => unknown; } export interface IViewportCollectorUpdateOptions { diff --git a/lib/utils.ts b/lib/utils.ts index e54f832..eeee6cf 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -108,7 +108,7 @@ export const warnNoContextAvailable = (location: string) => { if (process.env.NODE_ENV === 'production') { return; } - const fromHook = location.startsWith('use') + const fromHook = location.startsWith('use'); if (fromHook) { console.warn( `react-viewport-utils: ${location} hook is not able to connect to a . Therefore it cannot detect updates from the viewport and will not work as expected. To resolve this issue please add a as a parent of the component using the hook, e.g. directly in the ReactDOM.render call: @@ -130,8 +130,7 @@ ReactDOM.render( document.getElementById('root') );`, ); - return - + return; } console.warn( `react-viewport-utils: ${location} component is not able to connect to a . Therefore it cannot detect updates from the viewport and will not work as expected. To resolve this issue please add a as a parent of the component, e.g. directly in the ReactDOM.render call: