-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Working TS compiler & AST type checker #3677
Comments
related sveltejs/sapper#760 and #1639 |
Yep, there is already an issue about this. |
Not really the same issue. Yeah, what you just linked to is such a small part of actually implementing TS support.. |
So it really sounds like you guys don't care about TS support at ALL. Once you saw the title being named something with "TypeScript" you probably just closed it immediately. Should I continue this project to support TS fully, or are you planning to extend your compiler & checker with TS capabilities? |
I didn't link any issues. I should have linked to the relevant issue and I apologise for that. The issue I was referring to is this: #1639 I closed it because we already have an issue to discuss TypeScript support. Comments relating to TS support in Svelte components, what it should cover and how it could or should be implemented, should be directed the existing issue. We always close duplicate issues to reduce noise in the repository, having several issues for each feature request is not going to make dealing with them easier, however noteworthy the author thinks their issue is. Regarding Typescript, people do ask about it frequently and we, almost as frequently, respond that there is an issue to track it, we haven't got around to it yet and when we have any updates we will share them. If someone else wants to take this on, we welcome contributions from the community. |
As a TS dev since 2012, I actually don't care about TS support given how much JS tooling has improved. |
@quantuminformation that's why I'm asking whether or not they're going to support it, or if I should continue on my own. I only gotta take some precautions on whether or not the html AST is going to include breaking changes in the future. |
@pngwn cheers. I guess I'll continue my project then 😄 |
There can’t be a definite answer now as there are still so many other
priorities
On Thu, 10 Oct 2019 at 19:19, Marcus S. Abildskov ***@***.***> wrote:
@quantuminformation <https://github.com/QuantumInformation> that's why
I'm asking whether or not they're going to implement it, or if I should
continue on my own.
For me it doesn't really matter, I just want some clarification.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#3677?email_source=notifications&email_token=AABU35VKMKSQUI7TJKB2JOLQN5W3PA5CNFSM4I7CKFK2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEA5JCZY#issuecomment-540709223>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABU35TQTXVUDSJY4ZGAL43QN5W3PANCNFSM4I7CKFKQ>
.
--
Kind regards
Nikos Katsikanis
|
I did not realise that. I'll reopen this issue so further discussion on this solution can take place. This isn't an invitation for everyone to ask 'is it ready yet', so let's keep things focused on the solution at hand. There is definitely a desire for first-class typescript support, we don't necessarily have strong opinions on how that would work right now. As you can see from this comment this is something we want to do. But as you can also see from this comment we havent gotten around to it. Nothing has changed materially since the last comments in that issue were made. |
Bazel 🤔 ... did you mean Babel? |
@mindplay-dk no I meant Bazel :) |
This is awesome! 👍 It even tracks child component props usage in the parent, something I am always missing! This issue should probably be renamed to something like "Working Typescript Compiler" to show that you are actually suggesting a solution, instead of just asking for TS plans. Can you explain a bit simpler of how your solution works? As I understand, first you compile the scripts and templates with TS while caching their AST and generating declaration files per component, then let Svelte compile the components, and then do the actual type-checking on the compiled JS? |
@siosu I'll try to create a more descriptive diagram over the process (it's complex), but yes, you're close. Compiler:
It consumes the exported variables and functions from inside a Svelte script, and then generates it as a declaration files which contains a class that extends the runtime SvelteComponent. Typechecker:
This means that if you're using a 3rd party library that offers Svelte templates, they should either:
import { SvelteComponent } from 'svelte/internal';
export default class Alert extends SvelteComponent {
type: 'success' | 'info' | 'warning' | 'error';
} |
For a Typescript preprocessor, see https://github.com/kaisermann/svelte-preprocess#typescript. |
@miguelsan not the same and is pretty irrelevant to the thread, as I assume most people know that there's already 5-6 TS preprocessors for Svelte. |
EDIT: Ignore what I say here. I got corrected bellow. My take on this is that it we shouldn't be working on this support from the Svelte side. The same way the JSX Syntax is supported by Typescript compiler, Svelte should be too.
Babel, as a compiler, supports JSX, at compile time with the addition of a A good point was made on this comment on Typescript's issue tracking. Typescript should support Svelte |
@VictorGaiva have you even read the thread you're commenting on? |
@marcus-sa , thank you for the explanation. |
It would be nice it you could highlight the main difference between those preprocessors and this "TypeScript support": I find it hard to understand the purpose of this, since you only describe the how, and it even takes time to follow you on that. |
@miguelsan This project aims to check types on expressions in the template and across component boundaries as opposed to other somewhat popular projects that only check types within the <script> tag of a single .svelte component. That makes it a lot more useful to many people. |
from the video demo in the readme of svelte-ts, it seems working already, what else are missing? want to help on this |
For those asking why this approach is needed. Here is an example of the Type-Errors that this approach would catch, over the script tag preprocessor-only approach: // Inner.svelte
<script lang="ts">
export let names: string[];
</script>
<ul>
{#each names as name}
<li>{name}</li>
{/each}
</ul> // Outer.svelte
<script lang="ts">
import Inner from './Inner.svelte';
</script>
<Inner names="Alice,Bob" /> // Error! type 'string' is not compatible with type 'string[]' |
Typescript support is blocking me from using Svelte, so this is appreciated! |
This is exactly what I'm looking for! |
I propose having Svelte support the If such an api were adopted with Svelte, the following would apply.
|
@halfnelson that's pretty cool, but isn't it quite slow? |
svelte2tsx is pretty fast, typescript is slow yes :) which is why it is left as a stand-alone-tool here. It checks the svelte site src folder in about 3sec,
|
echoing @sinapis question, would the svelte team consider adopting either @marcus-sa’s or @halfnelson’s approaches? If not, what would be a feasible approach to perform the same checks and get this sort of "complete" type safety? happy to contribute! cheers |
I'm new here, so forgive me if I'm rehashing something obvious. I'm wondering if this approach would be worth pursuing, or if it has been done. For the case of Svelte components imported into That is to say, this code in a
... would trigger the generation of this
Personally speaking, this would be a "good-enough" for me, at least until there is first-class support. The typescript compiler could compile this into javascript as an intermediate step, and the normal svelte compiler could take it from there. What am I missing? |
@fredrik-macrobond a lot of people do, don't overwhelm everyone by spamming messages. To express yourself please use this: If you want to subscribe to the topic updates, please use this: If you have some meaningful information regarding the topic or want to help (frustrating everyone with meaningless notifications isn't helping), then welcome to the messages :) |
Hi, maybe there is another approach to support typescript. Due to the compiler nature of svelte, we can treat any svelte file, i.e. *.svelte as compilation target, and then write code in typescript and compile each *.tsx to *.svelte. The problem is that currently we have to use tool such as webpack or preprocessor to wrestle with both template and script. What we want is COMPLETE support for typescript, and only TOOLS can't acheive this goal perfectly. Thus it seems appealing to solve this problem in LANGUAGE level. Recently, I implemented typedraft, which is a superset of typescript with built-in support for code transformation, and it's relatively easier to do translation in this new language. The solution is repo svelte-draft, a single CLI tool, *.tsx in and *.svelte out. Also, I find that it's possible to get complete support for typescript in this way, and it 's just a matter of time. There are some samples:
Here is the analogy for this approach:
|
I have rewritten almost all examples in https://svelte.dev/tutorial with full power of typescript in this new approach. Repo: https://github.com/mistlog/svelte-draft-tutorial. |
@mistlog sorry to say, but that's not using svelte templates... |
Wow nice work! Works really well! I tried a whole bunch of the examples and it all works well. Example 14 / Slot Props doesnt compile tho for some reason. |
Thanks for your comment! |
I'm sorry and it's related to mistlog/svelte-draft-template#3. |
Yea, one of my main concerns about Svelte, - it's overloaded with some template code out of the box. It was mentioned many times that Svelte is inspired by NextJS many times. Though, if you'll create NextJS project: npx create-next-app You'll get a fine structured folder, with a few code dependencies in package.json, with just 2 example pages [you'll later replace], and no any configs at all. Though, if you'll want to create a Svelte project...npx degit sveltejs/template my-svelte-project
And what do I get in a newly created project? Yes, a similar example page, which I will replace with my real page, but what else?.. Rollup config? Some And, for the sake of justice, I admit, - it's already much better than it was months (a year?) ago. Earlier, there was also a For typescript support you'll also need Best practices, anyone?So in our programming world we have this good practice of having no configs required. A good practice of having default values. A good practice of conventions over configurations. A good practice of encapsulations. And by breaking these laws, it brings a lot of frustration when one uses such instruments. And I suppose it's very important thing here in this issue, "no configs", - that what it means "to have first-class typescript support". Otherwise it's not first class, - maybe some economy class, having you to say: "yes, we support typescript but...". Yet, don't get me wrong, - a lot of guys here did real good work toward typescript support here. This TS-based compiler, and this tutorial rewrite... I really appreciate your guys work, and willingness to use typescript, - I like typescript too! I just point to important problems I see with these typescript template repositories some of you share here. I understand "then do it yourself" concept, I'm just not ready to. Currently, pointing to where's the problem is much easier for me than implementing a solution here, - it's real hard, and that's why I so much appreciate your guys work here! I hope, I helped someone to see things a bit clearer. Thx for attention :) |
So I’ve been watching this issue and look forward to native typescript support if that’s possible (or an officially endorsed plugin that supports typescript). One thing that struck me as odd here was the discussion of “best practices”. While best practices help teams stay consistent, blindly picking “best practices” will lead to unnecessary religious debates that boil down to opinion, legacy, and laziness rather than what’s actually “best”. Best practices in one context can be horrible practices in another context. Let’s table “best practice” debates until we settle on the right solution that balances the needs of the svelte team and the needs of users. Edit: One need would be simplified configuration. The trade-off here is that the "simpler" the configuration is, the harder it is for users to control, and the more complex it is for power users to update. Think of something like |
Based on your videos and description (I haven't tried it yet) you've got working TS for Svelte. As we know, TS makes it far easier to stay on top of changes once it comes to refactoring, even though it's a bit slower to write in the first place, which is why a lot of people would want it before migrating (or starting) business critical code on Svelte. And we already know that @Rich-Harris wants TS in Svelte. But your PR hasn't attracted a single comment from a core team member! Though - of course - the comments you have got from some of the authorised project collaborators are very welcome. So... I've previously made the mistake of dropping a big update - which I really did and do think added something useful (and some agreed with me!) - onto an open source project without discussing it thoroughly first... and I agree, yours really is a useful update! But my experience is that (unfortunately, when the update in question really is useful to project users) this either gets ignored or ruffles feathers; and fairly so, in hindsight. In this case, there's already work afoot to integrate TypeScript #1639, so I'm sure they'd want to understand: is your work compatible with what's been done so far? And probably even more crucially, can it achieve whatever goals the team has already decided are most important for TS in Svelte to achieve? I wonder if there's any way you could try to establish direct contact with the team to discuss this? There's the Discord chatroom, but perhaps another approach would be to try a short, polite, public PM on this channel, very politely and modestly pointing out that you have working cross-component Typescript in Svelte, linking to your two videos (which kind of prove the point!), and asking whether Rich would be interested in discussing it more over PM, and potentially working together? (It sounds like you're definitely prepared to put the time in, over the long term, if the team want to work with you - which is obviously good!) (One other obvious question, how far can you go with integrating your work into as close as possible to the standard Svelte build chain?) m8, thanks, it looks very cool! Good luck! Mike |
I have analyzed Svelte commits for last half a year. Please correct me if I am wrong, but there is no a single commit related to Typescript support. There is no slightest sign of any progress in the area. I would like to choose Svelte for a project, but I cannot argue it is the right choice. I would like to know if Svelte team has any roadmap and vision of implementing TS. I also want to know whether they just do care. Just tell us when there will be any progress in the direction PS. They don't care |
@AlgoTrader I would personally assume the team just has different priorities. Is there any chance of using this as a wrapper around whatever version of Svelte? In that case the work on both could proceed independently. |
Guys, does simple TypeScript to JavaScript compiling and then triggering the svelte compiler work? |
@alpham8 Yes, if you don't want type-checking between components; but you almost certainly do, and it's not enough if you do. |
Hi folks, I've chatted with Rich a bunch about TS + JS with Svelte and came up with a proposal which lets people add TS to Svelte apps, and for folks to start building upon the existing tooling infrastructure to provide a rich editor experience. This lets the current svelte contributors continue to work on the compiler while opening up the chance for more people to start working on centralized tooling to improve editor experience (which includes TS support) If you've commented in here, you might be the sort of person who can help make it happen 👍 |
SPOILER: 999% that below is something very stupid Has anyone already suggested to NOT adding TS support and instead implement JS->JS transpiler? |
The folks want TypeScript, no matter if you like it or not... |
@yahorsi Yes, 4 comments above yours in this issue, and several other places further above, and elsewhere in linked issues. |
This reads harshly. I believe "JS->JS" was a typo and poster meant to write "TS->JS" |
This maybe side tracking, but want to share to the peers. If you're looking for TS/TSX with svelte, I suppose you're looking for a 'no-vdom' reactive programming experience with TSX. In this case, surplus[1] maybe what you're looking for. I just tried out it recently and like it a lot. |
Oh, my bad, I'm terribly sorry. I didn't wanna be nasty. Just keep friendly and polite and stick together for the coding goal here 😄 👍 |
In progress via #4518 |
I've seen multiple requests for TypeScript support.
This can be done in a few ways:
The second approach is something I've taken a shot at, quite successfully I must say.
The compiler transpiles all scripts into valid JS that the Svelte compiler can transform into components and then it creates declaration files for the components, that the type checking then can consume later on to validate the components and templates.
It saves the AST compilation in a cache which then later on, the TS type checker is used to validate everything.
What's your opinion on this?
The repository can be found at https://github.com/marcus-sa/svelte-ts
The text was updated successfully, but these errors were encountered: