-
-
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 Roadmap via LSP #4518
Comments
My experience with the Typescript compiler API is limited to a few hours of poking around, but I would love to help out with this once the repositories are all set up. |
I would love to help out with this. How can I get involved? |
I'm going to give it at least the week to sit on the chance someone has a good blocker. I'd like to get an explicit "yes, this is OK" from @UnwrittenFun - because while it's legal to fork, and he's said it is a good idea before. I'd rather be sure. In the meantime, I'll make a yarn workspace repo on my own user account which retains all the history of svelte-vscode and svelte-language-server and look into how their testing setup works. This can get transferred over as the base to work from. As homework, I'd recommend reading the code for: For folks who might be thinking this feels super complex types of code, don't worry, it really tends to be quite shallow code built on top of vscode-languageserver (because creating a LS is a reasonably common thing nowadays) - and a lot of the foundations are already set up. |
I'd also love to lend a hand! |
Sounds great! There is a project that has gone down this path that might be a great starting point: |
Thanks @halfnelson - I bet both of those tools have a lot of underlaying ideas we can get merged into the official LS tooling level! /cc @simlrh |
Exciting stuff, absolutely a "yes, this is OK" from me 👍 |
Happy to share lessons learned or answer questions, but I wouldn't have time to commit any code as I don't even find enough time to develop Vetur/VLS. Just a notice that transpiling gets expensive and complex soon. Consider auto completion. Transpilation need to happen in < 50ms in the worst case for completion to show up fast enough for each keystroke. Besides, your compiler needs to reasonably transpile all invalid states while user inputs svelte-specific JS such as |
@orta I have pretty good svelte/typescript language server running locally using I think it's definitely the place to start, but does need some work - it's pretty incredible what @halfnelson achieved using string manipulation with https://github.com/rich-harris/magic-string, but I have some worries about correctness in edge cases, so I began a full rewrite of |
Cool, so an update - doesn't feel like any blockers to the idea of merging and centralizing so far (other than, "there is more cool stuff we should integrate" - which is cool, we should try get that in) I've started merging some of the tooling repos together: https://github.com/orta/language-tools and updating all the dependencies. If there's still no conceptual blockers I'll start poking folks to get this pushed into a new remote of @octref's point about |
I think there are 3 related issues here: First just to clarify, this is valid JS and TS, the Second is how to type check the let x: number;
$: x = count + 1; Third is the issue of parsing the invalid file while the user is typing. |
For the let c = $(()=> count + 1); Would be easier for TypeScript, as there's no magic syntax. |
Shouldn't this work?
This could simply be translated to:
|
Trying to parse this as TS gives an error, it seem to treat both In my humble opinion I'd be in favour of a not adding any new syntax, and doing the minimum to make existing svelte type checkable, hence simply adding |
@simlrh I thought the script content will be transformed before it will be checked by typescript. That's why I've written:
|
Alright, doesn't sound like any blockers. Can an org admin please:
I'm happy to handle incoming PRs for a while to help sveltejs/langauge-tools get bootstrapped (you can add me to the org, or make me a contributor to that repo), but I don't use svelte nor have constructive production experience with it. So ideally, like to hope that some of the folks in this issue end up taking over those responsibilities with some time - I'll be here to provide moral support :D |
@orta you should have an org invite now giving you the access you need to transfer the repo(s) into the svelte org. 🚀 |
Thanks @antony! Over the weekend I've built out https://github.com/sveltejs/language-tools and got it working as a solid set of foundations. Tests/CI/Debugging/etc. Next up, this week, I'll try test a bunch of the tools with the community website and some open source examples and try give a sense of what a roadmap could look like for the tools in issues sveltejs/language-tools#6 You don't need to wait on me if you already have ideas - create issues! I'll be learning from scratch, so that comes with fresh perspective but does mean that I don't know what it's like to be doing something like this for a long time or for working on larger projects. |
Just plugging this here in case it (or its concept) helps with the |
I would love to help out in this. How can I help? |
This would be a breaking change given that variables prefixed with $ indicates syntax for store variables. |
@GrygrFlzr So, unite them -> reactive prefix. |
@daarxwalker |
@malj I know, what you mean, developers naming preferences, migrations to new syntax, etc... But Svelte isn't so standard thing, it's literally revolutionary, so if you add one simple special rule...don't see any problem. Honestly, who uses |
Plus, Svelte as compiler (framework) doing things with own way, has some strict rules and developer have to follow these rules. |
@daarxwalker The suggestion is indeed valid, and could potentially resolve some issues with TypeScript, but the community sentiment is likely to keep the label syntax, even if an alternative was also added. |
@marcusnewton |
It's pretty hard to gauge community sentiment if all the thumbs up here points towards replacing the However, I'd like to say in my opinion explicit syntax is better than implicit. After all you have to import all the other stuff from svelte too e.g. I actually don't care too much how it's solved, it's just a bit verbose for a typescript right now, e.g.
Looks a lot of syntax to me. |
@Ciantic let $urlUE: string = '...' |
What's the problem to be solved regarding TypeScript again? Are we talking about this example from earlier in this thread:
These type annotations on the labels could be recognized and stripped out by the Babel parser. (See https://babeljs.io/docs/en/babel-parser#will-the-babel-parser-support-a-plugin-system for one possibility; another would be if it could become officially supported syntax enabled via an official plugin, like how it works with JSX). The trickier question is how to actually do type checking on these. I think the labels would have to be transformed somehow to code that the TypeScript type-checker recognizes. This would complicate IDE integration as well. Just some things to keep in mind while exploring options... |
The way TypeScript support works is that svelte-preprocess processes the file and passes it to the TypeScript compiler and then to the Svelte compiler. We can probably support any syntax as long as svelte-preprocess handles it correctly. svelte-preprocess needs to be able to operate very quickly (for intellitype, autocomplete, etc. in the IDE) and today is just a regex, so it's best to keep that in mind while designing a syntax. |
@benmccann how about we do just this then:
before passing to typescript turn it to this:
TypeScript should be able to even infer the type. Then when it comes from Typescript, transpile back to Right now the biggest issue with |
OK, this thread has gone pretty far off topic and I'm going to lock it. You're welcome to create individual threads about their own topics. |
Alright, popping back to give basically a final update. Today we declared the VS Code extension production worthy, and recently svelte-preprocessor became an official svelte project. In my opinion, this is enough to call TypeScript support 1st class for Svelte.
There is both official support for making TS files work in the compiler, and work in your editor. There's a few docs + template issues to sort out but we're there and it took a bunch of work from a lot of folks to get there. Specifically @dummdidumm, @jasonlyu123, @UnwrittenFun, @halfnelson and @octref who provided code, foundations and valuable advice. Y'all did great work. Thank you. |
There we go, that's the right button. Issues with how bits work can go on either the IDE level in the language-tools repo, or at the svelte compiler level in svelte-preprocess. Docs issues can be new issues on this repo like normal 👍 |
This comment has been minimized.
This comment has been minimized.
Hi all. Great work on this! Just getting into Svelte and TS is a huge bonus for me. Are there any issues that can be watched for when documentation will be added? |
The docs are here: https://svelte.dev/faq#what-about-typescript-support though I'm seeing an outdated version and not what's at https://github.com/sveltejs/svelte/blob/master/site/content/faq/500-what-about-typescript-support.md |
Hi everyone, contratulations con the great work I followed this document and also tried starting directly with this template and I'm getting the following error when trying to import a .ts file from App.svelte: App.svelte <script lang="ts">
import { todos } from './stores'
export let name: string;
console.log('todos', todos)
</script> Error:
And this is my stores.ts export const todos: number[] = [ 1, 2, 3] Am I missing anything? |
@opensas You should open a ticket in the language-tools repo |
Time to re-lock this thread I guess 👋 - this is all prod ready, we've just not had a big announcement for it all, you can read the announcement bits here: #5101 |
Goal: TypeScript-style tooling in Svelte
Getting TS in Svelte might be a blocker for some, but I think the biggest underlaying advantage comes in that the "TS support" is just a stricter version of the JS tooling. Everyone using Svelte gets improvements in tooling from this, not just folks with TS.
This proposal makes a strong separation between the tooling and the svelte compiler. It explicitly minimizes the impact on core in favor of moving live-DX complexity to a svelte language server.
The language server provides editor support and CLI validation for working in svelte, not the core library svelte. This isn't a unique concept, it's based on the vue eco-system which has similar "extend HTML" constraints.
This pattern also allows different maintainers to work on JS/TS tooling independent of the svelte core team (which is how we do it in GraphQL), making it a good place for people who want to contribute to step up and own some of the svelte core ecosystem.
Three main steps to get there:
Add support in core for transpiling TypeScript in tags script, but ignore type-checking. TS will always synchronously emit JS if it's reasonably legal JS. This is the only change required in core. When you encounter a TS script tag, you can look up whether they have TS/Babel in deps and throw if not. No need to force the dep.
Create "svelte/svelte-language-tools" repo in main org:
svelte-language-server
svelte-vscode
Svelte Language Server improvements
Rationale
This is to allow the editor support to make that assumption out of the box, and for a user to be able to safely write
<script lang="typescript">
knowing that it will work with the core.Adding TypeScript support to Svelte can take a lot of tips from prior art done on Vue.
Vetur acts as the higher level tooling framework for both providing the JS and TS tooling support in scripts.
This pattern keeps most of the work in the LSP level, which means all editors can use it, the vscode extension would just be one client of many.
The language server could turn into
@svelte/language-server
and thevscode-svelte
can get some more maintainers added so that deploys can be done by others. If folks have other popular IDE integrations, they could live in this repo too.svelte check
adds a way to validate on CI, which can handle folks without the extension not accidentally breaking things. Because TS will always emit JS even if it has type errors, on purpose.This is basically a "draw the rest of the owl"
Work on Svelte Lanaguge Server
Luckily, both vscode-svelte and svelte-langauge-server have already been built and worked on by @UnwrittenFun and he's interested in moving those to the org. They both are solid foundations to build upon, from looking through the code.
They're definitely good enough for JS today, and considerably better than nothing!
I think one of the first blockers on making it something recommended by default, is finding a way to make sure that money-labels (reactive designators) are handled in both JS and TS.
Because the
$:
syntax does more work than TypeScript can know about$: x = count + 1
would probably need transforming into something likeconst x = () => { count + 1 }
as an in-memory transform, so that the variables exist in the "TS representation" of the LSP (but not on disk)HTMLx
The quick route:
I think you can get autocompletion, but I'm not sure how feasible typechecking would be here in a cheap way.
For inside
{}
the LSP could make API requests to the known script tags above and request all the variables at the top level scope (basically the equivilient of adding a newline in that script and then checking what's available.)In theory you could type check each one by making a unique TS document for all of them, with exposed globals from the main script - but that could be quite slow, and it's hard to say how well that could scale.
Longest term:
How TypeScript handles the LSP-ish server is by embedding it into the core codebase, perhaps over time it makes sense to give the LSP deeper access to the svelte compiler, so that the LSP can ask it questions about the document "what variables are available in this eval context?" as (I guess) you must have some of that information to do the compilation.
I'd see this as quite a long time away, and you can get a lot of value right now in the LSP without access to that.
The svelte language server could eventually split into a htmlx-language-server and the svelte version would wrap with svelte specific cases.
Things to think about
This opens up a lot of legitimate questions "like how does the tooling support x and y" - the answer to most of them is "that is something the language-server would have to handle" - the language server would need to keep track of files, their exports and more - it'll be a cool an interesting project for folks who care about the DX at editor level.
Anything in vetur today should be feasible for svelte.
/cc @octref who works on Vetur, and might have some insight.
The text was updated successfully, but these errors were encountered: