Skip to content

Commit

Permalink
FEAT: stricter TS - step 1 (#288)
Browse files Browse the repository at this point in the history
* [#286] Upgrade to TS4, fix type errors

* [#285] TS: strict:true, noImplicitThis: true

* [#285] TS: strictBindCallApply: true, useUnknownInCatchVariables: true

* [#285] Improve debounce() readability
  • Loading branch information
darkwebdev committed Nov 16, 2023
1 parent 2e610a6 commit c364212
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 15 deletions.
15 changes: 8 additions & 7 deletions src/common/debounce.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
export function debounce(fn: (any) => any, ms: number): () => void {
let timer
return () => {
export function debounce<F extends(...args: Parameters<F>) => ReturnType<F>>(
fn: F,
ms: number
): (...args: Parameters<F>) => void {
let timer: ReturnType<typeof setTimeout>

return (...args) => {
clearTimeout(timer)
timer = setTimeout((...args) => {
timer = null
fn.apply(this, ...args)
}, ms)
timer = setTimeout(() => fn(...args), ms)
}
}
6 changes: 3 additions & 3 deletions src/ebay-carousel/__tests__/index.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import { ComponentMeta } from '@storybook/react';
import { EbayCarousel, EbayCarouselItem } from '../index';
import { ComponentMeta, Story } from '@storybook/react'
import { EbayCarousel, EbayCarouselItem, CarouselProps } from '../index';

const story: ComponentMeta<typeof EbayCarousel> = {
component: EbayCarousel,
Expand Down Expand Up @@ -54,7 +54,7 @@ export const Continuous = (args) => {
);
};

const _ItemsPerSlide = (args) => {
const _ItemsPerSlide: Story<CarouselProps> = (args) => {
return (
<EbayCarousel gap={16} {...args}>
{items}
Expand Down
2 changes: 1 addition & 1 deletion src/ebay-carousel/carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ListItemRef } from './types'
import { getMaxOffset, getNextIndex, getOffset, getSlide } from './helpers'
import { debounce } from '../common/debounce'

type CarouselProps = ComponentProps<'div'> & {
export type CarouselProps = ComponentProps<'div'> & {
className?: string;
gap?: number;
index?: number; // Scroll to index slide
Expand Down
2 changes: 1 addition & 1 deletion src/ebay-carousel/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { default as EbayCarousel } from './carousel'
export { default as EbayCarousel, CarouselProps } from './carousel'
export { default as EbayCarouselItem } from './carousel-item'
4 changes: 2 additions & 2 deletions src/ebay-pagination/pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ const EbayPagination: FC<PaginationProps> = ({
const debouncedUpdate = debounce(updatePages, 16)

updatePages()
window.addEventListener('resize', debouncedUpdate)
window.addEventListener('resize', () => debouncedUpdate())

return () => {
window.removeEventListener('resize', debouncedUpdate)
window.removeEventListener('resize', () => debouncedUpdate())
}
}, [children])

Expand Down
10 changes: 9 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@
"outDir": "./lib",
"declaration": true,
"declarationMap": true,
"strict": false,
"strict": true,
"strictBindCallApply": true,
"strictFunctionTypes": false, // 21 errors
"strictNullChecks": false, // 150+ errors
"strictPropertyInitialization": false,
"useUnknownInCatchVariables": true,
"noImplicitAny": false, // 300+ errors
"noImplicitThis": true,
"alwaysStrict": true,
"moduleResolution": "node",
"baseUrl": "./",
"allowSyntheticDefaultImports": true,
Expand Down

0 comments on commit c364212

Please sign in to comment.