Skip to content

Commit 5a87c8f

Browse files
author
Jannick Garthen
committed
fix(*): use unknown instead of any type for layout snapshots
1 parent 4cff725 commit 5a87c8f

File tree

7 files changed

+21
-24
lines changed

7 files changed

+21
-24
lines changed

examples/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,5 +181,5 @@ setInterval(() => {
181181
</main>
182182
</ViewportProvider>,
183183
document.getElementById('root'),
184-
)
185-
}, 1000)
184+
);
185+
}, 1000);

lib/ObserveViewport.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ interface IState extends IChildProps {}
2828

2929
interface IProps {
3030
children?: (props: IChildProps) => React.ReactNode;
31-
onUpdate?: (props: IChildProps, layoutSnapshot: any) => void;
32-
recalculateLayoutBeforeUpdate?: (props: IChildProps) => any;
31+
onUpdate?: (props: IChildProps, layoutSnapshot: unknown) => void;
32+
recalculateLayoutBeforeUpdate?: (props: IChildProps) => unknown;
3333
disableScrollUpdates: boolean;
3434
disableDimensionsUpdates: boolean;
3535
deferUpdateUntilIdle: boolean;
@@ -98,7 +98,7 @@ export default class ObserveViewport extends React.Component<IProps, IState> {
9898
cancelAnimationFrame(this.tickId);
9999
}
100100

101-
handleViewportUpdate = (viewport: IViewport, layoutSnapshot: any) => {
101+
handleViewportUpdate = (viewport: IViewport, layoutSnapshot: unknown) => {
102102
const scroll = this.props.disableScrollUpdates ? null : viewport.scroll;
103103
const dimensions = this.props.disableDimensionsUpdates
104104
? null

lib/ViewportProvider.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ const getCurrentDefaultViewport = (() => {
3030
defaultValue = {
3131
scroll: getClientScroll(),
3232
dimensions: getClientDimensions(),
33-
}
33+
};
3434
}
35-
return defaultValue
36-
}
35+
return defaultValue;
36+
};
3737
})();
3838

3939
export const ViewportContext = React.createContext({
40-
removeViewportChangeListener: (handler: TViewportChangeHandler) => { },
40+
removeViewportChangeListener: (handler: TViewportChangeHandler) => {},
4141
addViewportChangeListener: (
4242
handler: TViewportChangeHandler,
4343
options: IViewportChangeOptions,
44-
) => { },
44+
) => {},
4545
getCurrentViewport: getCurrentDefaultViewport,
4646
hasRootProviderAsParent: false,
4747
version: '__VERSION__',
@@ -84,7 +84,7 @@ const shouldSkipIteration = (
8484
export default class ViewportProvider extends React.PureComponent<
8585
IProps,
8686
{ hasListeners: boolean }
87-
> {
87+
> {
8888
static defaultProps: {
8989
experimentalSchedulerEnabled: false;
9090
};
@@ -145,7 +145,7 @@ export default class ViewportProvider extends React.PureComponent<
145145
if (recalculateLayoutBeforeUpdate) {
146146
const getDuration = createPerformanceMarker();
147147
const layoutState = recalculateLayoutBeforeUpdate(state);
148-
return [layoutState, getDuration()];
148+
return [layoutState, getDuration()] as const;
149149
}
150150
return null;
151151
},

lib/hooks.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ interface IEffectOptions<T> extends IOptions {
2121
recalculateLayoutBeforeUpdate?: (viewport: IViewport) => T;
2222
}
2323

24-
type HandleViewportChangeType = (viewport: IViewport, snapshot: any) => void;
25-
26-
export const useViewportEffect = <T = any>(
27-
handleViewportChange: HandleViewportChangeType,
24+
export const useViewportEffect = <T>(
25+
handleViewportChange: (viewport: IViewport, snapshot: T) => void,
2826
options: IViewPortEffectOptions<T> = {},
2927
) => {
3028
const {
@@ -58,7 +56,7 @@ export const useViewport = (options: IFullOptions = {}): IViewport => {
5856
return state;
5957
};
6058

61-
export const useScrollEffect = <T = any>(
59+
export const useScrollEffect = <T = unknown>(
6260
effect: (scroll: IScroll, snapshot: T) => void,
6361
options: IEffectOptions<T> = {},
6462
) => {
@@ -80,7 +78,7 @@ export const useScroll = (options: IOptions = {}): IScroll => {
8078
return scroll;
8179
};
8280

83-
export const useDimensionsEffect = <T = any>(
81+
export const useDimensionsEffect = <T = unknown>(
8482
effect: (scroll: IDimensions, snapshot: T) => void,
8583
options: IEffectOptions<T> = {},
8684
) => {
@@ -124,7 +122,7 @@ export const useRect = (
124122
);
125123
};
126124

127-
export const useLayoutSnapshot = <T = any>(
125+
export const useLayoutSnapshot = <T = unknown>(
128126
recalculateLayoutBeforeUpdate: (viewport: IViewport) => T,
129127
options: IFullOptions = {},
130128
): null | T => {

lib/modules.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,4 @@ interface DOMRectReadOnly {
6666
toJSON: () => any;
6767
}
6868

69-
declare module "@testing-library/react"
69+
declare module '@testing-library/react';

lib/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export interface IViewportChangeOptions {
4646
notifyDimensions: () => boolean;
4747
notifyOnlyWhenIdle: () => boolean;
4848
priority: () => PriorityType;
49-
recalculateLayoutBeforeUpdate?: (viewport: IViewport) => any;
49+
recalculateLayoutBeforeUpdate?: (viewport: IViewport) => unknown;
5050
}
5151

5252
export interface IViewportCollectorUpdateOptions {

lib/utils.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export const warnNoContextAvailable = (location: string) => {
108108
if (process.env.NODE_ENV === 'production') {
109109
return;
110110
}
111-
const fromHook = location.startsWith('use')
111+
const fromHook = location.startsWith('use');
112112
if (fromHook) {
113113
console.warn(
114114
`react-viewport-utils: ${location} hook is not able to connect to a <ViewportProvider>. Therefore it cannot detect updates from the viewport and will not work as expected. To resolve this issue please add a <ViewportProvider> as a parent of the component using the hook, e.g. directly in the ReactDOM.render call:
@@ -130,8 +130,7 @@ ReactDOM.render(
130130
document.getElementById('root')
131131
);`,
132132
);
133-
return
134-
133+
return;
135134
}
136135
console.warn(
137136
`react-viewport-utils: ${location} component is not able to connect to a <ViewportProvider>. Therefore it cannot detect updates from the viewport and will not work as expected. To resolve this issue please add a <ViewportProvider> as a parent of the <ObserveViewport> component, e.g. directly in the ReactDOM.render call:

0 commit comments

Comments
 (0)