-
Notifications
You must be signed in to change notification settings - Fork 150
typescript does not recognize svelte syntax #144
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
Comments
I just want to solve the build error. |
@benmccann I try to add a test case, and it will fail. it('should apply "$: " syntax', async () => {
const { diagnostics } = await transpile(
`
$: anything = "test";
`,
);
expect(diagnostics.some(d => d.code === 2304)).toBe(false);
}); |
Isn't this for diagnostic code 2552? Diagnostic code here is 2304, and the message is different:
I'm seeing this too when using typescript with svelte-preprocess. |
Hmm. It looks like the example app I was testing on has a workaround of declaring the variable first: Or maybe they just do that to declare the type? It's a good question of how do you declare the type on a reactive variable like that. You'd probably want to be able to do |
Yeah, I just put the example from the Svelte docs in a TypeScript app:
It failed:
|
It looks like TypeScript has multiple "Cannot find name" diagnostic codes I wonder if some others like |
This does remove the issue - so you need to declare the variable first for it to be recognized by TypeScript. Makes sense - TypeScript compiler is running before Svelte, and this is not a valid TypeScript syntax. Is this valid JS? |
My understanding is the preprocessor pulls the code out of the I'm not affiliated with this project, btw. I've just spent some time of my own trying to get TypeScript stuff working. So I might be wrong on some of this @mervinjin's test case above is a good one. I wonder if someone just changed the diagnostic code check and added that test case if it'd be enough to get things working and send in a PR |
It would definitely work, but should another workaround be added for this? I'd rather document this as something that needs to be handled a bit differently on TS side. |
I don't think it's a workaround. I think that's how this was actually meant to be implemented. We're telling TypeScript to ignore the |
I see the point. $: is actually valid JS syntax - it's just that this is not valid for TS compiler. So you need to handle that also as a special case on preprocess side or user needs to declare the variable before the label. It's just that the number of special cases handled for each supported preprocessor could become overwhelming. |
We have to do something special to handle I'm still wondering the best way to handle this. Neither Svelte nor TypeScript say how the two should be combined. We basically have to invent that here. The most natural would be:
However, neither TypeScript nor Svelte like that. We can deal with the TypeScript part here, but the Svelte part might require support from the Svelte team. The following seems to work now:
I think we could also make the following work by only changing the preprocessor:
|
Based off the discussion in sveltejs/svelte#4965 I'm convinced that nothing should change in Svelte itself. There was a suggestion there that we use the two line split declaration and initialization syntax:
The good thing about this option is that it doesn't introduce new syntax that's specific to the pre-processor and is already supported today. We could just close this ticket by adding better documentation. It is more verbose, but I haven't written enough code with the combination of TypeScript and Svelte to know whether it'd be terribly annoying or not. |
Hey there, thanks for the thread. My two cents on this:
$: name = segment + 'aa'; // error TS2588: Cannot assign to 'name' because it is a constant. So as @benmccann suggests, you just need to declare a variable in the scope of the component for the error to go away. |
Describe the bug
typescript does not recognize svelte syntax when I use Sapper.
Logs
syntax error reporting:
build error reporting:

To Reproduce
repo: https://github.com/mervinjin/vache/tree/svelte-ts-issue
Expected behavior
should have no error.
Information about your project:
Your operating system: OS X 10.15.4
svelte-preprocess
version : v3.7.4Whether your project uses Webpack or Rollup: yes, I use rollup
The text was updated successfully, but these errors were encountered: