|
| 1 | +# TypeScript Support |
| 2 | + |
| 3 | +## Getting it to work in the editor |
| 4 | + |
| 5 | +To tell us to treat your script tags as typescript, add a `type` or `lang` attribute to your script tags like so: |
| 6 | + |
| 7 | +```html |
| 8 | +<!-- Add type="text/typescript" --> |
| 9 | +<script type="text/typescript"> |
| 10 | + export let name: string; |
| 11 | +</script> |
| 12 | + |
| 13 | +<!-- Or add lang="typescript" or lang="ts" --> |
| 14 | +<script lang="typescript"> |
| 15 | + export let name: string; |
| 16 | +</script> |
| 17 | +``` |
| 18 | + |
| 19 | +You may optionally want to add a `svelte.config.js` file (see below) - but it is not required as long as you only use TypeScript. |
| 20 | + |
| 21 | +## Getting it to work for your build |
| 22 | + |
| 23 | +For the editor, this is already enough - nothing more to do. But you also need to enhance your build config. Using Rollup, this will work with Svelte and TypeScript as long as you enable `svelte-preprocess` and `@rollup/plugin-typescript`: |
| 24 | + |
| 25 | +- Install these packages `npm i -D svelte-preprocess typescript tslib @rollup/plugin-typescript` |
| 26 | +- Add these lines to `rollup.config.js`: |
| 27 | + |
| 28 | +```js |
| 29 | +// ... |
| 30 | +import sveltePreprocess from 'svelte-preprocess'; |
| 31 | +import typescript from '@rollup/plugin-typescript'; |
| 32 | + |
| 33 | +// ... |
| 34 | + plugins: [ |
| 35 | + svelte({ |
| 36 | + // ... |
| 37 | + preprocess: sveltePreprocess(), // <-- |
| 38 | + }), |
| 39 | + |
| 40 | + // ... |
| 41 | + commonjs(), |
| 42 | + typescript(), // <-- added below commonjs |
| 43 | + // ... |
| 44 | +``` |
| 45 | + |
| 46 | +- Add a `tsconfig.json` with these lines: |
| 47 | + |
| 48 | +```json |
| 49 | +{ |
| 50 | + "include": ["src/**/*"], |
| 51 | + "exclude": ["node_modules/*", "__sapper__/*", "public/*"], |
| 52 | + "compilerOptions": { |
| 53 | + "moduleResolution": "node", |
| 54 | + "sourceMap": true, |
| 55 | + "target": "es2017", |
| 56 | + "types": ["svelte"] |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +And this should work to enable full TypeScript checking in your Svelte files. For further discussion and a clonable template [see this issue](https://github.com/sveltejs/language-tools/issues/161). |
| 62 | + |
| 63 | +## Example configuration for the editor |
| 64 | + |
| 65 | +#### Using [svelte-preprocess](https://github.com/sveltejs/svelte-preprocess) |
| 66 | + |
| 67 | +##### Install |
| 68 | + |
| 69 | +```sh |
| 70 | +npm i -D svelte-preprocess typescript |
| 71 | +``` |
| 72 | + |
| 73 | +<details> |
| 74 | +<summary>Yarn</summary> |
| 75 | + |
| 76 | +```sh |
| 77 | +yarn add --dev svelte-preprocess typescript |
| 78 | +``` |
| 79 | + |
| 80 | +</details> |
| 81 | + |
| 82 | +##### Set up `svelte.config.js` |
| 83 | + |
| 84 | +```js |
| 85 | +const sveltePreprocess = require('svelte-preprocess'); |
| 86 | + |
| 87 | +module.exports = { |
| 88 | + preprocess: sveltePreprocess(), |
| 89 | +}; |
| 90 | +``` |
| 91 | + |
| 92 | +##### Restart the svelte language server |
| 93 | + |
| 94 | +You will need to tell svelte-vscode to restart the svelte language server in order to pick up the new configuration. |
| 95 | + |
| 96 | +Hit `ctrl-shift-p` or `cmd-shift-p` on mac, type `svelte restart`, and select `Svelte: Restart Language Server`. Any errors you were seeing should now go away and you're now all set up! |
| 97 | + |
| 98 | +## Troubleshooting / FAQ |
| 99 | + |
| 100 | +### How do I type reactive assignments? / I get an "implicitly has type 'any' error" |
| 101 | + |
| 102 | +The following code may throw an error like `Variable 'show' implicitly has type 'any' in some locations where its type cannot be determined.`, if you have stricter type settings: |
| 103 | + |
| 104 | +```html |
| 105 | +<script lang="typescript"> |
| 106 | + export let data: { someKey: string | null }; |
| 107 | +
|
| 108 | + $: show = !!data.someKey; // <-- `show` has type `any` |
| 109 | +</script> |
| 110 | + |
| 111 | +{#if show}hey{/if} |
| 112 | +``` |
| 113 | + |
| 114 | +To type the variable, do this: |
| 115 | + |
| 116 | +```ts |
| 117 | +let show: boolean; // <--- added above the reactive assignment |
| 118 | +$: show = !!data.someKey; // <-- `show` now has type `boolean` |
| 119 | +``` |
| 120 | + |
| 121 | +### How do I import interfaces into my Svelte components? I get errors after transpilation! |
| 122 | + |
| 123 | +- If you use `svelte-preprocess` BELOW `v4.x` and did NOT set `transpileOnly: true`, then make sure to have at least `v3.9.3` installed, which fixes this. |
| 124 | +- If you don't use `svelte-preprocess` OR use `transpileOnly: true` (which makes transpilation faster) OR use `v4.x`, import interfaces like this: `import type { SomeInterface } from './MyModule.ts'`. You need a least TypeScript 3.8 for this. |
| 125 | + |
| 126 | +### Can I use TypeScript syntax inside the template/mustache tags? |
| 127 | + |
| 128 | +At the moment, you cannot. Only `script`/`style` tags are preprocessed/transpiled. See [this issue](https://github.com/sveltejs/svelte/issues/4701) for more info. |
| 129 | + |
| 130 | +### I get weird type errors on my html tags |
| 131 | + |
| 132 | +This may be due to some library you are using having installed typings for `react`. These are picked up by the TypeScript compiler. Because we internally transform svelte to jsx, there is a clash and said error occurs. |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | +The underlying [issue in TypeScript](https://github.com/microsoft/TypeScript/issues/18588) is yet to be fixed but in the meantime, one way to work around it is as follows: |
| 137 | + |
| 138 | +1. Add a `sink.d.ts` with content `declare module 'react' {}` |
| 139 | +2. Go to your `tsconfig.json` |
| 140 | +3. If you do not have such a setting already, enhance `compilerOptions` with `"baseUrl": "."` |
| 141 | +4. Enhance `compilerOptions` with `"paths": { "react": ["<path to your sink.d.ts, relative to the baseUrl>"] }` |
| 142 | + |
| 143 | +`tsconfig.json` |
| 144 | + |
| 145 | +```json |
| 146 | +{ |
| 147 | + "compilerOptions": { |
| 148 | + "baseUrl": ".", |
| 149 | + "paths": { |
| 150 | + "react": ["sink.d.ts"] |
| 151 | + } |
| 152 | + } |
| 153 | +} |
| 154 | +``` |
| 155 | + |
| 156 | +`sink.d.ts:` |
| 157 | + |
| 158 | +```ts |
| 159 | +declare module 'react'; |
| 160 | +``` |
| 161 | + |
| 162 | +For more info see [this issue](https://github.com/sveltejs/language-tools/issues/228) |
0 commit comments