Difference between how Svelte and JS imports from node_modules are handled
See original GitHub issueThrough #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 asany
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:
- Created 2 years ago
- Reactions:2
- Comments:8 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 doingimport { emit } from '@tauri-apps/api/event'
in a.ts
file and then runningtsc --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).Yes. You could check
.ts
files withtsc --noEmit
however. But that might produce some false positives, depending on whether or not you have named imports from Svelte files inside TS files.