River Ray @ leizhenpeng.com

TwoSlash with Shikiji

Dec 12, 2023 · 2min

Here is an example of using TwoSlash with Shikiji:

var console: Consoleconsole.(method) Console.log(...data: any[]): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log)
log
((1 + 2 + 3 + 4).to
  • toExponential
  • toFixed
  • toLocaleString
  • toPrecision
  • toString
Fixed
(2))
interface interface TodoTodo { (property) Todo.title: stringtitle: string } const const todo: Readonly<Todo>todo: type Readonly<T> = { readonly [P in keyof T]: T[P]; }
Make all properties in T readonly
Readonly
<interface TodoTodo> = {
(property) title: string
title
: 'Delete inactive users',
} const todo: Readonly<Todo>todo.title = 'Hello'
Cannot assign to 'title' because it is a read-only property.

Try hovering on each token to see the type information. Shiki runs oniguruma to give syntax highlighting, and TwoSlash runs typescript to give type information. Both are quite heavy libraries, but this is done ahead of time on building, it means that what you see here is completely static pure HTML and CSS!

In addition to the original shiki-twoslash package, the Shikiji integration allows you to provide custom renderer of how the HTML been generated based on AST. This allows us to have dual themes support, and also the syntax highlighting for the type information.

Check shikiji-twoslash and commit for the integration on leizhenpeng.com, for more information :)

The input code
```ts twoslash
// @errors: 2540
console.log((1 + 2 + 3 + 4).toFixed(2))
//                            ^|

interface Todo {
  title: string
}

const todo: Readonly<Todo> = {
  title: 'Delete inactive users',
//  ^?
}

todo.title = 'Hello'
```
> comment on mastodon / twitter
>
CC BY-NC-SA 4.0 2024-PRESENT © River Ray