Twoslash
This feature integrate Twoslash
All of the Typescript code blocks would provide inline type hover.
Enable twoslash
- Install @sveltepress/twoslash package
npm install --save @sveltepress/twoslash
- Config
highlighter.twoslash
totrue
import { defaultTheme } from '@sveltepress/theme-default'
import { sveltepress } from '@sveltepress/vite'
import { defineConfig } from 'vite'
export default defineConfig ({
plugins : [
sveltepress ({
theme : defaultTheme ({
highlighter : {
twoslash : true
}
})
})
]
})
Basic type annotation
const foo = false
const obj = {
a : 'a',
b : 1
}
```ts
const foo = false
const obj = {
a: 'a',
b: 1
}
```
Errors
const foo : Foo = null
const a: number = '1'
```ts
// @errors: 2304 2322
const foo: Foo = null
const a: number = '1'
```
Queries
const hi = 'Hello'
const msg = `${hi }, world`
//
//
```ts
const hi = 'Hello'
const msg = `${hi}, world`
// ^?
//
//
```
Cut codes
Cut before
use // ---cut---
or // ---cut-before---
can cut all codes before this line
console .log (level )
```ts
const level: string = 'Danger'
// ---cut---
console.log(level)
```
Cut after
use // ---cut-after---
can cut all codes after this line
console .log (level )
```ts
const level: string = 'Danger'
// ---cut-before---
console.log(level)
// ---cut-after---
console.log('This is not shown')
```
Cut start/end
use // ---cut-start---
and // ---cut-end---
to cut contents between them
const level : string = 'Danger'
console .log ('This is shown')
```ts
const level: string = 'Danger'
// ---cut-start---
console.log(level) // This is not shown.
// ---cut-end---
console.log('This is shown')
```
Twoslash for svelte
<script>
import { onMount } from 'svelte'
let { message = 'World' } = $ props ()
let count = $ state (0)
onMount (() => {
})
</script>
<button onclick={() => count ++}>
Count is: {count }
</button>
<div class="text-6">
Hello, {message }
</div>