TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Twoslash: Markup for generating rich type information in documentation

26 pointsby bpierreover 1 year ago

2 comments

dimitropoulosover 1 year ago
here&#x27;s an in-depth video with the (de-facto) author of twoslash for anyone interested <a href="https:&#x2F;&#x2F;youtu.be&#x2F;An_XkLxLWjA?si=M8Cehe8xtYAi6_FH" rel="nofollow">https:&#x2F;&#x2F;youtu.be&#x2F;An_XkLxLWjA?si=M8Cehe8xtYAi6_FH</a><p>also, did you know that there&#x27;s an inline syntax? it&#x27;s `&#x2F;&#x2F; =&gt;`
killcoderover 1 year ago
We&#x27;ve been using (the old) Shiki Twoslash for a few years now for our docs pages:<p><a href="https:&#x2F;&#x2F;electricui.com&#x2F;docs&#x2F;components&#x2F;LineChart" rel="nofollow">https:&#x2F;&#x2F;electricui.com&#x2F;docs&#x2F;components&#x2F;LineChart</a><p><a href="https:&#x2F;&#x2F;electricui.com&#x2F;docs&#x2F;operators&#x2F;aggregations" rel="nofollow">https:&#x2F;&#x2F;electricui.com&#x2F;docs&#x2F;operators&#x2F;aggregations</a><p>Our product, Electric UI, is a series of tools for building user interfaces for hardware devices on desktop. It has a DataFlow streaming computation engine for data processing which leans heavily on TypeScript&#x27;s generics. It&#x27;s pretty awesome to be able to have examples in our docs that correctly show the types as they flow through the system. I certainly learn tools faster when they have good autocomplete in the IDE. Twoslash helps bring part of that experience earlier in the development process, right to when you&#x27;re looking at documentation.<p>Our site is built with GatsbyJS, the docs are a series of MDX files rendered statically, then served via Cloudflare Pages. We use the remark plugins to statically render the syntax highlighting and hover tag information, then some client-side React to display the right tooltips on hover.<p>We build a Twoslash environment from a tagged commit of our built TypeScript definitions, from the perspective of our default template. The Twoslash snippets as a result have all the required context built in, given they are actual compiled pieces of code. The imports we display in the docs are the actual imports used when compiling from the perspective of a user. It bothers me when docs only give you some snippet of a deeply nested structure, and you don&#x27;t know where to put it. Even worse when it&#x27;s untyped JS! Using Twoslash lets us avoid that kind of thing systematically.<p>The CI system throws errors when our docs snippets &quot;don&#x27;t compile&quot;, which is very helpful in keeping our docs up to date with our product. Nothing worse than incorrect docs!<p>We use React components extensively, and I&#x27;m not really happy with our prop reference tables which use Palintir&#x27;s Documentalist. Our components are increasingly using complex TypeScript generics to represent behaviour. The benefits in the IDE are huge, but the relatively dumb display of type information in the prop table leaves something to be desired. I&#x27;m most likely going to replace the data for those tables with compile-time generated Twoslash queries.<p>My only complaints have been around absolute speed of compilation, but I haven&#x27;t dug deep into improving that. I just set up a per snippet caching layer, and once files are cached, individual changes are refreshed quickly. After all, it&#x27;s invoking the full TypeScript compiler, and that&#x27;s its biggest feature.<p>Overall I&#x27;ve been very happy with Twoslash, and I&#x27;m looking forward to refactoring to use this successor and Shikiji (the ESM successor to Shiki), hopefully it improves our performance. The new Twoslash docs are look great, a huge improvement on when we started using it.