question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Difference between how Svelte and JS imports from node_modules are handled

See original GitHub issue

Through #1048, I noticed there is a difference between how JS and Svelte files imported from node_modules are handled:

  • Imports from node_modules that have no accompanying type definition (d.ts file) are marked as any which errors on strict mode (no implicit any). This means TS does not attempt to aquire the type structure from the node_modules package and also does not type-check it
  • Svelte imports from node_modules are read and treated as a regular file inside the project. This means they don’t need a accompanying type definition and their types are aquired, which can be good or bad depending on the quality of the Svelte file. Some libraries like svelte-loading-spinners take advantage of this and provide TS-versions of Svelte files as types, which is dangerous.

I think in the long run it would be best to align with the behavior of TypeScript here. This would probably a breaking change to some users which do deep imports of Svelte files within libraries which don’t have a .svelte.d.ts next to them. I’m also not sure how to even achieve this as I thought this stuff is handled inside ts.resolveModuleName. As an intermediate step, we definetly need to stop type-checking stuff inside node_modules (also see #1100) @jasonlyu123 thoughts?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
dummdidummcommented, Sep 13, 2021

The diagnostics you get from node_modules are from real TypeScript files. These files will be type-checked by TypeScript/tsc, too. You can try yourself by doing import { emit } from '@tauri-apps/api/event' in a .ts file and then running tsc --noEmit. There’s also an issue about this in the TypeScript repo, with the current conclusion that this is “works as designed”: https://github.com/microsoft/TypeScript/issues/40426. This sounds more like a misconfiguration of the build step of the library (why are .ts files deployed?) or a misuse of the library (avoid importing the .ts files).

1reaction
dummdidummcommented, Jul 16, 2021

Yes. You could check .ts files with tsc --noEmit however. But that might produce some false positives, depending on whether or not you have named imports from Svelte files inside TS files.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Modules • Docs • SvelteKit
A subdirectory of $lib . SvelteKit will prevent you from importing any modules in $lib/server into client-side code. See server-only modules. $service ...
Read more >
Working with Svelte stores - Learn web development | MDN
Svelte stores offer similar features for state management. ... stores.js' import { onDestroy } from 'svelte' let alertContent = '' const ...
Read more >
Learn How SvelteKit Works As a Framework - Joy of Code
The svelte-kit and vite binaries are under node_modules/.bin that get executed when you run the development server and if you open the file...
Read more >
Importing a JS npm library in Svelte
The package you're importing imports its own package.json. Rollup won't handle JSON imports by default, so you will need to install the json ......
Read more >
Documentation - TypeScript 4.5
Supporting lib from node_modules. To ensure that TypeScript and JavaScript support works well out of the box, TypeScript bundles a series of declaration ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found