From dcc78947b32948092fc40e4b6c5ce971e6ef0964 Mon Sep 17 00:00:00 2001 From: Yaroslav Kiliba Date: Sun, 7 Feb 2021 17:51:40 +0200 Subject: [PATCH] chore: add ts to workflow --- .github/workflows/test.yml | 4 +++- package.json | 2 +- src/index.ts | 14 ++++++++------ tsconfig.build.json | 3 ++- tsconfig.json | 2 +- 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index fbfcd52..7086630 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -35,6 +35,8 @@ jobs: npm rm react react-dom npm i react@${{ matrix.react-version }} react-dom@${{ matrix.react-version }} - name: Validate - run: npm run lint + run: | + npm run tsc + npm run lint - name: Run tests run: npm run cy:ci diff --git a/package.json b/package.json index 86947a4..cf5bfa3 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "dev": "next", "test": "cypress open -b chrome", "lint": "eslint --fix --ext .ts,.tsx src components pages", - "tsc": "tsc", + "tsc": "tsc -p tsconfig.build.json", "cy:run": "cypress run", "cy:ci": "start-server-and-test dev http://localhost:3000 cy:run" }, diff --git a/src/index.ts b/src/index.ts index ccf7f6f..adac270 100644 --- a/src/index.ts +++ b/src/index.ts @@ -38,7 +38,9 @@ const getLazyInitImages = (): string[] => { } return normInitImages; }; -const isInitImage = (src: string | undefined): boolean => getLazyInitImages().some((initImgSrc) => initImgSrc?.endsWith(src)); +const isInitImage = (src: string | undefined): boolean => getLazyInitImages().some( + (initImgSrc) => src && initImgSrc?.endsWith(src), +); const isInitSsr = (isSsrMode = false, src?: string): boolean => { if (isSsrMode && src && isBrowser && document.readyState !== 'complete' && isInitImage(src)) { return true; @@ -46,11 +48,11 @@ const isInitSsr = (isSsrMode = false, src?: string): boolean => { return false; }; -function normArg(obj: ImgArg | string): Omit | { 'srcset': ImgArg['srcSet'] } +function normArg(obj: ImgArg): Omit | { 'srcset': ImgArg['srcSet'] } +function normArg(obj: SourceArg): Omit | { 'srcset': SourceArg['srcSet'] }[] function normArg(obj: SourceArg): Omit | { 'srcset': SourceArg['srcSet'] }[] function normArg(obj: any): any { - const nObj = typeof obj === 'string' ? { src: obj } : obj; - const normObj = { ...nObj, srcset: nObj.srcSet }; + const normObj = { ...obj, srcset: obj.srcSet }; delete normObj.srcSet; Object.keys(normObj).forEach((key) => { if (normObj[key] === undefined) { @@ -78,7 +80,7 @@ const useProgressiveImage = ({ // eslint-disable-next-line consistent-return const image = useDeepCompareMemo(() => { - const src = typeof imgArg === 'string' ? imgArg : imgArg.src; + const src = typeof imgArg === 'string' ? imgArg : imgArg?.src; if (src && isBrowser && !isInitSsr(ssr, src)) { const img = document.createElement('img'); @@ -94,7 +96,7 @@ const useProgressiveImage = ({ img.onload = rerender; img.onerror = handleError; - Object.assign(img, normArg(imgArg)); + Object.assign(img, normArg(typeof imgArg === 'string' ? { src: imgArg } : imgArg!)); return img; } }, [imgArg, sourcesArg, rerender, handleError, ssr]); diff --git a/tsconfig.build.json b/tsconfig.build.json index 093b143..b931acf 100644 --- a/tsconfig.build.json +++ b/tsconfig.build.json @@ -42,7 +42,7 @@ // "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */ /* Module Resolution Options */ - // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ + "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ @@ -67,6 +67,7 @@ "skipLibCheck": true, /* Skip type checking of declaration files. */ "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ }, + "include": ["src"], "exclude": [ "node_modules" ] diff --git a/tsconfig.json b/tsconfig.json index e6e64c2..99628c7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,7 +8,7 @@ ], "allowJs": true, "skipLibCheck": true, - "strict": false, + "strict": true, "forceConsistentCasingInFileNames": true, "noEmit": true, "esModuleInterop": true,