Skip to content

Commit b126f67

Browse files
committed
Tighten the post
1 parent 2fe853b commit b126f67

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

site/content/blog/2020-06-04-svelte-and-typescript.md

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ author: Orta Therox
55
authorURL: https://twitter.com/orta
66
---
77

8-
It's been on the TODO list for a while, and it's now happening. TypeScript with Svelte is now a first class citizen of the eco-system.
8+
It's been on the TODO list for a while, and it's now happening. TypeScript with Svelte is now a first class citizen of the ecosystem.
99

10-
We think it'll help you handle much larger Svelte code bases regardless of whether you use TypeScript.
10+
We think it'll help you handle much larger Svelte code bases regardless of whether you use TypeScript or JavaScript.
11+
12+
<figure>
13+
<img alt="Screenshot of TypeScript in Svelte" src="media/svelte-ts.png">
14+
<figcaption>Image of TypeScript + Svelte in VS Code (theme is <a href="https://marketplace.visualstudio.com/items?itemName=karyfoundation.theme-karyfoundation-themes">Kary Pro</a>.)</figcaption>
15+
</figure>
1116

1217
## What does it mean to support TypeScript in Svelte?
1318

@@ -17,7 +22,7 @@ A week before COVID was declared a pandemic, [I pitched a consolidation](https:/
1722

1823
#### How does it work?
1924

20-
To understand the two main parts of TypeScript support, we'll compare it to the technique TypeScript uses to provide dev tools. There is a compiler `tsc` which you run on the command-line to convert `*.ts` to `*.js`, then there is a `TSServer` which is a node API that responds to requests from text editors. The `TSServer` is what provides all the JavaScript and TypeScript experience, and it has most of the compiler's code inside it.
25+
To understand the two main parts of TypeScript support, we'll compare it to the technique TypeScript uses to provide dev tools. There is a compiler `tsc` which you run on the command-line to convert `*.ts` to `*.js`, then there is a `TSServer` which is a node API that responds to requests from text editors. The `TSServer` is what provides all the JavaScript and TypeScript realtime introspection for editors while coding, and it has most of the compiler's code inside it.
2126

2227
For Svelte, we have the Svelte compiler, and now we have the [`svelte-language-server`](https://github.com/sveltejs/language-tools/tree/master/packages/language-server#svelte-language-server) which responds to text editor calls via the [Language Server Protocol standard](https://microsoft.github.io//language-server-protocol/overviews/lsp/overview/). First class TypeScript support means that _both_ of these two systems do good job of handling TypeScript code.
2328

@@ -30,7 +35,8 @@ For the editor level, we took inspiration from [Pine's](https://github.com/octre
3035

3136
For the official Svelte VS Code extension, we built off the foundations which [James Birtles](https://github.com/UnwrittenFun) has created in [`UnwrittenFun/svelte-vscode`](https://github.com/UnwrittenFun/svelte-vscode) and [`UnwrittenFun/svelte-language-server`](https://github.com/UnwrittenFun/svelte-language-server/).
3237

33-
[Simon H](https://github.com/dummdidumm) and [Lyu, Wei-Da](https://github.com/jasonlyu123) have done great work improving the Javascript and Typescript introspection, including integrating [@halfnelson](https://github.com/halfnelson)'s [svelte2ts](https://github.com/sveltejs/language-tools/tree/master/packages/svelte2tsx#svelte2tsx) which powers understanding the props on components in your codebase.
38+
[Simon Holthausen](https://github.com/dummdidumm) and [Lyu, Wei-Da](https://github.com/jasonlyu123) have done great work improving the JavaScript and TypeScript introspection, including integrating [@halfnelson](https://github.com/halfnelson)'s [svelte2tsx](https://github.com/sveltejs/language-tools/tree/master/packages/svelte2tsx#svelte2tsx) which powers understanding the props on components in your codebase.
39+
3440

3541
### Try today
3642

@@ -43,6 +49,13 @@ node scripts/updateTypeScriptVersion.js
4349

4450
### Adding TypeScript support to your project
4551

52+
Before getting started, add the dependencies:
53+
54+
```bash
55+
yarn add --dev svelte-preprocess @rollup/plugin-typescript @tsconfig/svelte svelte-check
56+
npm install --save-dev svelte-preprocess @rollup/plugin-typescript @tsconfig/svelte svelte-check
57+
```
58+
4659
##### 1. Compiling TypeScript
4760

4861
You first need to set up [`svelte-preprocess`](https://github.com/sveltejs/svelte-preprocess#svelte-preprocess) and [`@rollup/plugin-typescript`](https://github.com/rollup/plugins/tree/master/packages/typescript#rollupplugin-typescript) for the compiler:
@@ -81,9 +94,9 @@ Your `include`/`exclude` may differ per project.
8194

8295
##### 2. Editor Support
8396

84-
Any editor which supports using an LSP can work, the [VS Code](https://marketplace.visualstudio.com/items?itemName=svelte.svelte-vscode) extension has been our primary focus but there is work in progress [on Atom](https://github.com/sveltejs/language-tools/pull/160), and [coc-svelte](https://github.com/coc-extensions/coc-svelte) has been updated with the latest changes.
97+
Any editor [using an LSP](https://langserver.org/#implementations-client) can be supported. The [VS Code](https://marketplace.visualstudio.com/items?itemName=svelte.svelte-vscode) extension has been our primary focus, but there is work in progress [on Atom](https://github.com/sveltejs/language-tools/pull/160), and Vim via [coc-svelte](https://github.com/coc-extensions/coc-svelte) has been updated with the latest LSP.
8598

86-
These editor extensions will improve your coding experience even if you only use JavaScript. The editor won't offer errors, but it will offer inference and refactoring tools. You can [add `// @check-js`](https://www.staging-typescript.org/docs/handbook/intro-to-js-ts.html) to the top of a `<script>` tag using JavaScript to get better error messages.
99+
These editor extensions will improve your coding experience even if you only use JavaScript. The editor won't offer errors, but it will offer inference and refactoring tools. You can [add `// @check-js`](https://www.staging-typescript.org/docs/handbook/intro-to-js-ts.html) to the top of a `<script>` tag using JavaScript to get better error messages with no infra changes.
87100

88101
To switch a `<script>` to use TypeScript, use `<script lang="ts">` and that should be it. Hopefully you won't be seeing an ocean of red squiggles.
89102

site/static/media/svelte-ts.png

185 KB
Loading

0 commit comments

Comments
 (0)