Description
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.