svelte: Typescript: Exported members from Svelte modules cannot be detected from Typescript files
Describe the bug
When using Typescript, exported properties from a component’s context="module" script can be imported in another .svelte file, but cannot be imported in a regular .ts file.
<!-- Page.svelte -->
<script lang="ts" context="module">
export const version = '1.0';
</script>
<!-- App.svelte -->
<script lang="ts">
import { version } from './Page.svelte'; // works fine
</script>
// index.ts
import { version } from './Page.svelte'; // [tsserver 2614] [E] Module '"*.svelte"' has no exported member 'version'. Did you mean to use 'import version from "*.svelte"' instead?
Logs
[tsserver 2614] [E] Module '"*.svelte"' has no exported member 'version'. Did you mean to use 'import version from "*.svelte"' instead?
To Reproduce Issue does not exist with Javascript Svelte, and cannot be reproduced on https://svelte.dev/repl.
Issue can be reproduced in this repository.
https://github.com/bryanmylee/svelte-ts-module-export
It is still possible to compile the code with Rollup despite the Typescript errors, however this breaks the build step when using Sapper and Webpack with Typescript.
Expected behavior
Import should work even from .ts file.
Stacktraces On a Sapper project with Webpack and Typescript:
✗ server
/Users/bryan/Projects/Programs/bryanmylee.github.io/src/node_modules/@my/components/PageTransitions/index.ts
./src/node_modules/@my/components/PageTransitions/index.ts
[tsl] ERROR in /Users/bryan/Projects/Programs/bryanmylee.github.io/src/node_modules/@my/components/PageTransitions/index.ts(3,10)
TS2614: Module '"*.svelte"' has no exported member 'version'. Did you mean to use 'import version from "*.svelte"' instead?
✗ client
/Users/bryan/Projects/Programs/bryanmylee.github.io/src/node_modules/@my/components/PageTransitions/index.ts
./src/node_modules/@my/components/PageTransitions/index.ts
[tsl] ERROR in /Users/bryan/Projects/Programs/bryanmylee.github.io/src/node_modules/@my/components/PageTransitions/index.ts(3,10)
TS2614: Module '"*.svelte"' has no exported member 'version'. Did you mean to use 'import version from "*.svelte"' instead?
Information about your Svelte project:
System:
OS: macOS 11.0.1
CPU: (8) x64 Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
Memory: 454.03 MB / 16.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 14.4.0 - ~/.nvm/versions/node/v14.4.0/bin/node
Yarn: 1.22.10 - /usr/local/bin/yarn
npm: 6.14.9 - ~/.nvm/versions/node/v14.4.0/bin/npm
Browsers:
Chrome: 87.0.4280.88
Edge: 87.0.664.66
Firefox: 81.0.1
Safari: 14.0.1
npmPackages:
svelte: ^3.17.3 => 3.31.0
typescript: ^4.0.3 => 4.1.3
webpack: ^4.7.0 => 4.44.2
Severity Medium severity.
I can workaround this by moving context="module" exports to a Typescript file instead. However, it does break my design pattern of putting all sub-components of a component in a folder, and re-exporting the main component and its interfaces via a index.ts file.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 21
- Comments: 16 (5 by maintainers)
Commits related to this issue
- Update jest, svelte-jest, ts-jest. Install svelte2tsx for better it's type defintions. Moves types from component module to ts files. Reasoning: Unit tests fail when types are imported in a ts file f... — committed to mathesar-foundation/mathesar by pavish 5 years ago
- typefile and 'rollup-plugin-typescript2' upgrade - buildfix - updated 'rollup-plugin-typescript2' for: 'Unknown object type "asyncfunction" - added type file for: has no exported member https://gith... — committed to christianhugoch/ANovokmet-svelte-gantt by christianhugoch 3 years ago
- Explain about context=module exports into Typescript Document the issue from https://github.com/sveltejs/svelte/issues/5817 — committed to curiousdannii/svelte by curiousdannii 2 years ago
So basically, as I understand it, the only way for now is to ignore warnings or to manually redeclare all types inside a
.d.ts. I hope svelte finds a solution to this problem, pretty annoying to have 100+ warnings every time you save a file (this also will make real errors harder to spot). Maybe make svelte create the.d.tstype declaration files automatically for each svelte component that has exports in its module context ? That doesn’t look impossible to me, then again I don’t know in detail how svelte works under the hood. I don’t understand why this issue is closed though… EDIT: that’s why I opened another one #7304It’s only the typing that’s wrong here, you are able to use the imports like you want.
Reason: Svelte files are declared like this in the Svelte type definitions:
This means “Dear TypeScript, if you see a file ending with
.sveltein.tsor.jsfiles, use that definition”. That definition does not contain other exports, so TypeScript says “that is wrong”, even if it’s right in your case. To fix it, you can provide your own declaration inside a.d.tsfile:@Conduitry maybe we could relax the module declaration and just do
declare module '*.svelte';. Everything is typed asanythen - the disadvantage is that we no longer have a typed default export then. Maybe someone else knows how to get the best of both worlds.I have this problem in Svelte 5 where I do a module export for a type but I cannot import it in a
.svelte.tsfile (it works fine in.sveltefiles).For anyone still stuck with this problem, or Svelte modules and TypeScript in general, I use this approach which works fine.
For a given Svelte component which requires module context (eg. as a Singleton to share with other parts of the application), simply create a supplementary TypeScript file at the same file location. This is your module context, an actual TypeScript module. That way the Svelte component can import those shared values and read/write, just as any other part of the application can.
So for example for
MyComponent.svelte, create a file at the same project location calledMyComponent.tsand export any sharedconstor types/interfaces from there. Your structure would look something like:This is essentially what the
context="module"script directive is trying to achieve, so no harm in doing it with plain old TypeScript.Yes, it adds extra files, however if you keep the filenames the same and keep them both in the same project location it isn’t really a big problem. Considering that most Svelte components don’t need module context, and for the ones which do it’s technically easier for other devs to know that the
.tscompanion file is going to provide shared types or objects.Unfortunately there’s no way. A plugin like
typescript-svelte-pluginonly works for the IDE, the TS team explicitly disallows usage of plugins in the CLI, so you can’t tell rollup to use it.This actually reminds me of #442 on sveltejs/language-tools. Basically figuring out a better way to get support for advanced Component definitions such as generics.
I have spent several hours on this problem. It is a pity that there is no clean and simple solution for such a primitive problem. I don’t like the idea with an extra file (.d.ts), because I have to map my logic in it or lose type safety.
The following solution works in most of my use cases. Maybe it also helps you.
import { foo } from "./js/some-file.ts.svelte";That clears it up a lot, thank you.
I would love to contribute somehow to developing a Typescript version of the documentation, or a journal of best practices with Typescript + Svelte. I’m beginning to feel bad using the issue tracker so much to discover Typescript + Svelte features.