Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing types in @sveltejs/kit #5114

Closed
iva2k opened this issue May 30, 2022 · 6 comments
Closed

Missing types in @sveltejs/kit #5114

iva2k opened this issue May 30, 2022 · 6 comments

Comments

@iva2k
Copy link

iva2k commented May 30, 2022

Describe the bug

Setup

npm init svelte my-app
# choose:
# demo app
# TypeScript
# ESLint
# Prettier
# Playwright

cd my-app
pnpm install

Run check

pnpm run check
# no errors

Modify tsconfig.json:

{
    "extends": "./.svelte-kit/tsconfig.json",
    "compilerOptions": {
        "allowJs": true,
        "checkJs": true,
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "resolveJsonModule": true,
        "skipLibCheck": true,
        "sourceMap": true,
        "strict": true,
+        "outDir": ".types",
+        "paths": {
+            "$app/*": [".svelte-kit/runtime/app/*"],
+            "$lib": ["src/lib"],
+            "$lib/*": ["src/lib/*"]
+        }
    }
}

Run check with modified tsconfig.json

pnpm run check

Expected: no errors

Actual: 2 errors

The same 2 errors are thrown when just running tsc.

Reproduction

https://github.com/iva2k/sveltejs-kit-issue-types-20220529

Logs

C:\dev\svelte\my-app>pnpm run check
> my-app@0.0.1 check C:\dev\svelte\my-app
> svelte-check --tsconfig ./tsconfig.json

====================================
Loading svelte-check in workspace: c:\dev\svelte\my-app
Getting Svelte diagnostics...

c:\dev\svelte\my-app\.svelte-kit\runtime\client\singletons.js:1:19
Error: Cannot find module './types' or its corresponding type declarations.
/** @type {import('./types').Client} */
let client;

c:\dev\svelte\my-app\.svelte-kit\runtime\client\singletons.js:6:21
Error: Cannot find module './types' or its corresponding type declarations.
 * @param {{
 *   client: import('./types').Client;
 * }} opts

====================================
svelte-check found 2 errors, 0 warnings, and 0 hints
ELIFECYCLE  Command failed with exit code

System Info

System:
    OS: Windows 10 10.0.19044
    CPU: (8) x64 Intel(R) Core(TM) i7-1065G7 CPU @ 1.30GHz
    Memory: 4.17 GB / 15.74 GB
  Binaries:
    Node: 18.1.0 - C:\nodejs\node.EXE
    Yarn: 1.22.15 - C:\nodejs\yarn.CMD
    npm: 8.10.0 - C:\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.1266.0), Chromium (101.0.1210.53)
    Internet Explorer: 11.0.19041.1566
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.48
    @sveltejs/kit: next => 1.0.0-next.345
    svelte: ^3.46.0 => 3.48.0

Severity

serious, but I can work around it

Additional Information

Apparently, defining $app alias in tsconfig.json enables Typescript to analyze import('$app/navigation') in forms.ts, which pulls in files that lack Typescript types. Removing $app from tsconfig.json.compilerOptions.paths hides the problem,

I checked @sveltejs/kit repo, and found some *.d.ts files that are not bundled into the distributed package, and one of them (@sveltejs/kit/packages/kit/src/runtime/client/types.d.ts) contains interface Client, which is missing in the bundle.

This issue manifested itself when making a fix for #1560, and remains hidden until $app alias is defined.

@iva2k
Copy link
Author

iva2k commented May 30, 2022

Possibly related to work in #2514

@dummdidumm
Copy link
Member

dummdidumm commented May 30, 2022

This works as expected. $app/* imports are supposed to be typed through this ambient file which is picked up through the /// reference at the top of app.d.ts. $app/* types are not meant to be resolved through the generated code. Just remove the $app/* reference from your paths definition.

@iva2k
Copy link
Author

iva2k commented May 30, 2022

Thanks for quick response @dummdidumm. However...

Removing $app from paths breaks bunch of import lint checks in #1560. Which is not good to have unchecked and therefore possibly broken imports. Adding $app fixed all of these lint errors. Is there other way to have all imports linted in my proposed fix in #1560? ESLint does not seem to be picking up the /// reference.

But my point here is different - it is that typedef interface Client is not to be found in any .d.ts files in the packaged @sveltejs/kit, but it is used by the demo app. I inspected the code, found the typedef, and see that the typedef is not picked up by rollup config.

My ask here is to add typedef interface Client to the @sveltejs/kit package types.

@iva2k
Copy link
Author

iva2k commented Aug 28, 2022

Bump!
Please consider reopening.

ask here is to add typedef interface Client to the @sveltejs/kit package types, since it is exposed in the API, but not present in types. For one, ESLint is not finding the types based on /// reference.

@dummdidumm
Copy link
Member

Where is Client exposed in the API exactly? Not sure I follow.

@iva2k
Copy link
Author

iva2k commented Nov 12, 2022

@dummdidumm

Where is Client exposed in the API exactly? Not sure I follow.

The Client type is pulled from file ".svelte-kit/runtime/client/singletons.js"

which has this line at the top: /** @type {import('./types').Client} */

The file is created by Svelte-kit.

The steps to get the error are trivial and listed in the issue, I also posted a repro repo:
https://github.com/iva2k/sveltejs-kit-issue-types-20220529

Have you looked at it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants