Twoslash
该功能集成了 Twoslash
所有的 Typescript 代码块将会自动添加鼠标上浮的类型提示
开启 twoslash
- 安装 @sveltepress/twoslash 依赖
npm install --save @sveltepress/twoslash - 将默认主题
highlighter.twoslash选项设置为true
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
}
})
})
]
}) 基础类型注释
const foo = false
const obj = {
a : 'a',
b : 1
} ```ts
const foo = false
const obj = {
a: 'a',
b: 1
}
``` TS 编译错误
const foo : Foo = null
const a: number = '1' ```ts
// @errors: 2304 2322
const foo: Foo = null
const a: number = '1'
``` 类型查询
const hi = 'Hello'
const msg = `${hi }, world`
//
//
Number .p arseInt ('123', 10)
//
//
//
// ```ts
const hi = 'Hello'
const msg = `${hi}, world`
// ^?
//
//
Number.parseInt('123', 10)
// ^|
//
//
//
//
``` 自定义代码内提示
const a = 1自定义信息
const b = 1自定义信息
const c = 1自定义信息
自定义信息 ```ts
// @log: 自定义信息
const a = 1
// @error: 自定义信息
const b = 1
// @warn: 自定义信息
const c = 1
// @annotate: 自定义信息
``` 代码裁剪
向前裁剪
使用 // ---cut--- or // ---cut-before--- 注释可以将该行之前的所有代码从结果中裁剪调
console .log (level ) ```ts
const level: string = 'Danger'
// ---cut---
console.log(level)
``` 向后裁剪
使用 // ---cut-after--- 注释可以将该行之后的所有代码从结果中裁剪调
console .log (level )
```ts
const level: string = 'Danger'
// ---cut-before---
console.log(level)
// ---cut-after---
console.log('This is not shown')
``` 自定义裁剪段落
使用 // ---cut-start--- 与 // ---cut-end--- 注释可以指定裁剪这两个注释之间的所有代码
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')
``` 支持 svelte 代码块
<script>
import { onMount } from 'svelte'
let { message = '世界' } = $ props ()
let count = $ state (0)
onMount (() => {
})
</script>
<button onclick={() => count ++}>
你点击了 {count } 次
</button>
<div class="text-6">
你好, {message }
</div>