magic-akari / seamless-scroll-polyfill

Scroll Behavior polyfill
https://www.npmjs.com/package/seamless-scroll-polyfill
MIT License
159 stars 12 forks source link

Compiler errors when using with `noUncheckedIndexedAccess` #121

Closed Bessonov closed 3 years ago

Bessonov commented 3 years ago

I'm not sure why, but I get compile errors from ForkTsCheckerWebpackPlugin:

ERROR in ../../node_modules/.pnpm/seamless-scroll-polyfill@2.0.1/node_modules/seamless-scroll-polyfill/Element/scrollIntoView.ts:406:15
TS2532: Object is possibly 'undefined'.
    404 |
    405 |     return [
  > 406 |         top - scrollMarginTop,
        |               ^^^^^^^^^^^^^^^
    407 |         right + scrollMarginRight,
    408 |         bottom + scrollMarginBottom,
    409 |         left - scrollMarginLeft,

ERROR in ../../node_modules/.pnpm/seamless-scroll-polyfill@2.0.1/node_modules/seamless-scroll-polyfill/Element/scrollIntoView.ts:407:17
TS2532: Object is possibly 'undefined'.
    405 |     return [
    406 |         top - scrollMarginTop,
  > 407 |         right + scrollMarginRight,
        |                 ^^^^^^^^^^^^^^^^^
    408 |         bottom + scrollMarginBottom,
    409 |         left - scrollMarginLeft,
    410 |     ] as const;

ERROR in ../../node_modules/.pnpm/seamless-scroll-polyfill@2.0.1/node_modules/seamless-scroll-polyfill/Element/scrollIntoView.ts:408:18
TS2532: Object is possibly 'undefined'.
    406 |         top - scrollMarginTop,
    407 |         right + scrollMarginRight,
  > 408 |         bottom + scrollMarginBottom,
        |                  ^^^^^^^^^^^^^^^^^^
    409 |         left - scrollMarginLeft,
    410 |     ] as const;
    411 | };

ERROR in ../../node_modules/.pnpm/seamless-scroll-polyfill@2.0.1/node_modules/seamless-scroll-polyfill/Element/scrollIntoView.ts:409:16
TS2532: Object is possibly 'undefined'.
    407 |         right + scrollMarginRight,
    408 |         bottom + scrollMarginBottom,
  > 409 |         left - scrollMarginLeft,
        |                ^^^^^^^^^^^^^^^^
    410 |     ] as const;
    411 | };
    412 |

I looked into npm artifact and found some non-obvious code:

cat packages/frontend/node_modules/seamless-scroll-polyfill/lib/index.d.ts
export * from "../Element/index.js";
export * from "../Element/polyfill.js";
export * from "../Window/index.js";
export * from "../Window/polyfill.js";
export * from "./seamless-polyfill.js";
//# sourceMappingURL=index.d.ts.map

Because this is the first error of such type, I think it's more related to this library and not to the type checker plugin.

Reproducible example:

https://github.com/Bessonov/monorepo-starter/tree/seamless-scroll-polyfill

BTW, if I enable noUncheckedIndexedAccess in seamless-scroll-polyfill, then I get the same error on pnpm run type-check. One of the way to fix that is:

diff --git a/Element/scrollIntoView.ts b/Element/scrollIntoView.ts
index 55d1a17..709c72f 100644
--- a/Element/scrollIntoView.ts
+++ b/Element/scrollIntoView.ts
@@ -400,7 +400,12 @@ const getElementScrollSnapArea = (
         }
         const value = computedStyle.getPropertyValue(`${scrollProperty}-${edge}`);
         return parseInt(value, 10) || 0;
-    });
+    }) as [
+        number,
+        number,
+        number,
+        number,
+    ];

     return [
         top - scrollMarginTop,
diff --git a/tsconfig.json b/tsconfig.json
index 5529404..47a8025 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -9,7 +9,8 @@
         "downlevelIteration": true,
         "strict": true,
         "skipLibCheck": true,
-        "forceConsistentCasingInFileNames": true
+        "forceConsistentCasingInFileNames": true,
+        "noUncheckedIndexedAccess": true
     },
     "include": [".internal", "Element", "lib", "Window"]
 }

I can create a pull request, if it's desired,

github-actions[bot] commented 3 years ago

:tada: This issue has been resolved in version 2.0.2 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

Bessonov commented 3 years ago

@magic-akari wow, thank you very much for incredible fast resolution! This resolves my compilation issue. I think the problem with d.ts files is still here, but I don't see any issue with it.

github-actions[bot] commented 3 years ago

:tada: This issue has been resolved in version 2.1.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: