-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Typescript: Exported members from Svelte modules cannot be detected from Typescript files #5817
Comments
It'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: declare module '*.svelte' {
export { SvelteComponentDev as default } from 'svelte/internal';
} This means "Dear TypeScript, if you see a file ending with declare module '*.svelte' {
export { SvelteComponentDev as default } from 'svelte/internal';
export const version: string;
// ... other stuff
} @Conduitry maybe we could relax the module declaration and just do |
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. |
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. The following solution works in most of my use cases.
|
As stated above, this is a "TypeScript interop with non-TypeScript-files" issue. Solving it for VS Code required writing a dedicated TypeScript plugin, which is now available. More info here: sveltejs/language-tools#580 (comment) |
@bryanmylee, I don't suppose you have any insights as to why this works with rollup but not webpack? |
When using webpack or rollup with Svelte it's best to turn off their type checker and use |
Okay, I guess I misinterpreted the closing of this issue.
I'll try this approach. Thanks @dummdidumm. |
How do you turn off type cheking from the rollup config ? if you remove I use (!) Plugin typescript: @rollup/plugin-typescript TS2614: Module '"*.svelte"' has no exported member 'Product'. Did you mean to use 'import Product from "*.svelte"' instead?
src/stores/products.ts: (2:10)
2 import { Product } from "../components/Product.svelte" |
You can make the build ignore errors (it will still show them but not abort) https://github.com/rollup/plugins/tree/master/packages/typescript/#noemitonerror |
Thanks, since |
Unfortunately there's no way. A plugin like |
So basically, as I understand it, the only way for now is to ignore warnings or to manually redeclare all types inside a |
… type defintions. Moves types from component module to ts files. Reasoning: Unit tests fail when types are imported in a ts file from a component directly. See sveltejs/svelte#5817.
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
This is essentially what the 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 |
- buildfix - updated 'rollup-plugin-typescript2' for: 'Unknown object type "asyncfunction" - added type file for: has no exported member sveltejs/svelte#5817 (comment)
@Rich-Harris commented #7304 (comment)
Unfortunately nothing in the docs suggests that there are any gotchas involved with Can we at the very least treat this as a documentation issue? It seems that there is some important info that is not clearly stated anywhere:
I've hobbled together something that works mostly, but it's hard to know what I've missed. For my case at least, there's the simple solution of importing constants into Svelte, rather than exporting them to TS. Edit: I've made a PR suggesting a minimal docs change for 1. |
Document the issue from sveltejs#5817
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.Logs
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:
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 aindex.ts
file.The text was updated successfully, but these errors were encountered: