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.

Extreme explorations of TypeScript's type system

163 pointsby joshuakgoldbergalmost 3 years ago

15 comments

fishtoasteralmost 3 years ago
You can do some truly silly things with sufficiently ridiculous uses of typescript. I built a typecheck-time spell checker[0] in it such that:<p><pre><code> import { ValidWords } from &quot;.&#x2F;spellcheck&quot;; &#x2F;&#x2F; Typechecks cleanly: const result: ValidWords&lt;&quot;the quick brown fox.&quot;&gt; = &quot;valid&quot;; &#x2F;&#x2F; Throws a type error const result: ValidWords&lt;&quot;the qxick brown fox.&quot;&gt; = &quot;valid&quot;; </code></pre> [0] <a href="https:&#x2F;&#x2F;github.com&#x2F;kkuchta&#x2F;TSpell" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;kkuchta&#x2F;TSpell</a>
评论 #31903920 未加载
评论 #31902527 未加载
评论 #31903036 未加载
评论 #31901048 未加载
评论 #31900267 未加载
评论 #31902161 未加载
kevingaddalmost 3 years ago
When doing fancy things with typescript types, be really careful - it&#x27;s possible to accidentally construct typescript types that will increase your tsc compile times by <i>multiple seconds</i> and the tooling for troubleshooting this is nonexistent. A tiny change to one codebase I work on made compile times go from 300ms to something like 7 seconds and it took me something like 14 hours of grepping and manually bisecting source code to find the cause - tsc was O(N * N * N) trying all possible types for a string literal to determine whether any of them were valid matches, and someone had defined a <i>very</i> fancy string literal type.<p>When this happens, typescript language integration (like in vs code or sublime text) will suddenly fall over and stop working correctly, and it&#x27;ll be near impossible to figure that out too.<p>Our build uses rollup to invoke tsc and as it happens their profiling system doesn&#x27;t actually measure how long tsc takes to run - the time is unaccounted :) So in general, be aware that &#x27;typescript is taking a long time to compile&#x27; is a blind spot for this whole ecosystem and if you hit it you&#x27;re going to have to work hard to fix it.
评论 #31903240 未加载
dllthomasalmost 3 years ago
&gt; If you do find a need to use type operations, please—for the sake of any developer who has to read your code, including a future you—try to keep them to a minimum if possible. Use readable names that help readers understand the code as they read it. Leave descriptive comments for anything you think future readers might struggle with.<p>Also, as you start getting complicated logic in your types, you need to test your types; make sure they admit things they should admit and reject things that they should reject. Ideally these tests can also serve some role as examples for your documentation.
评论 #31899919 未加载
评论 #31901890 未加载
评论 #31899839 未加载
评论 #31926949 未加载
sir_pepealmost 3 years ago
To try and limit one&#x27;s use of operations on types, as suggested in the article, is not really great advice in my opinion. Sure, you would not want to actually implement and use a VM in types, but distilling rules about a program into types and then deriving the actual interfaces and signatures from those rules with operations on types? That&#x27;s quite powerful.<p>TypeScript&#x27;s type annotations are really a DSL embedded into JavaScript. And they can, and, depending on the problem at hand, <i>should</i> be treated as such.
评论 #31899742 未加载
评论 #31900992 未加载
评论 #31899405 未加载
tobralmost 3 years ago
&gt; You have to wonder whether you could implement TypeScript itself in that language...<p>I also wonder if you could compile TypeScript to TypeScript types? After all, you want your type manipulation code to be typesafe.
theogravityalmost 3 years ago
This is a great list. I feel I&#x27;m only scratching the surface when it comes to Typescript, and it would be awesome to have a place where we can see advanced examples of Typescript usage like this.<p>I&#x27;ve seen many projects where the typing is done so well that it can infer and include all the data I&#x27;ve fed into the TS-defined functions &#x2F; classes, which is great for IDE autocompletion.
评论 #31899261 未加载
evolveyourmindalmost 3 years ago
Some other type-only TS projects:<p>- RegExp matching through types: <a href="https:&#x2F;&#x2F;github.com&#x2F;desi-ivanov&#x2F;ts-regexp" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;desi-ivanov&#x2F;ts-regexp</a><p>- Lambda calculus through types: <a href="https:&#x2F;&#x2F;github.com&#x2F;desi-ivanov&#x2F;ts-lambda-calc" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;desi-ivanov&#x2F;ts-lambda-calc</a><p>- Brainfuck through types: <a href="https:&#x2F;&#x2F;github.com&#x2F;susisu&#x2F;typefuck" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;susisu&#x2F;typefuck</a>
评论 #31901974 未加载
pjnzalmost 3 years ago
I did some fiddling around building a graphql layer with a bunch of complex types. Basically this was trying to encode all the various GraphQL rules into the type system itself e.g. if a resolver takes arguments, ensure that a schema of the correct type is provided as an object etc. I also built a client that would take a schema and ensure you used it correctly at compile time.<p>Example of the code: <a href="https:&#x2F;&#x2F;github.com&#x2F;pj&#x2F;typeshaman&#x2F;blob&#x2F;main&#x2F;packages&#x2F;graphql&#x2F;src&#x2F;resolvers.ts" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;pj&#x2F;typeshaman&#x2F;blob&#x2F;main&#x2F;packages&#x2F;graphql&#x2F;...</a><p>Documentation is incomplete, unfortunately I had to get a job. I started working on encoding all of SQL as well.
DonatelloTHMalmost 3 years ago
These are some of my &quot;Typescript type system&quot; based projects:<p>Wordle: <a href="https:&#x2F;&#x2F;codesandbox.io&#x2F;s&#x2F;wordle-typescript-d4srgi?file=&#x2F;src&#x2F;index.ts" rel="nofollow">https:&#x2F;&#x2F;codesandbox.io&#x2F;s&#x2F;wordle-typescript-d4srgi?file=&#x2F;src&#x2F;...</a><p>Anadrome(Anagram Palindrome): <a href="https:&#x2F;&#x2F;codesandbox.io&#x2F;s&#x2F;anagram-palindrome-7u14xr?file=&#x2F;src&#x2F;index.ts" rel="nofollow">https:&#x2F;&#x2F;codesandbox.io&#x2F;s&#x2F;anagram-palindrome-7u14xr?file=&#x2F;src...</a><p>Candy Crush 1D: <a href="https:&#x2F;&#x2F;codesandbox.io&#x2F;s&#x2F;candycrush-u2v5pr?file=&#x2F;src&#x2F;index.ts" rel="nofollow">https:&#x2F;&#x2F;codesandbox.io&#x2F;s&#x2F;candycrush-u2v5pr?file=&#x2F;src&#x2F;index.t...</a>
adamddev1almost 3 years ago
Are there (m)any other languages with type systems as flexible and powerful as Typescript&#x27;s?
评论 #31900342 未加载
评论 #31901816 未加载
评论 #31899656 未加载
评论 #31901509 未加载
bichiliadalmost 3 years ago
If you&#x27;re going down the rabbit hole of writing complex types, check out Dan Vanderkam&#x27;s &quot;The Display of Types&quot; post[0]. It goes into how types show up in editors and error messages and such, and has a bunch of tricks for improving type readability. I really wish I read it sooner!<p>[0]: <a href="https:&#x2F;&#x2F;effectivetypescript.com&#x2F;2022&#x2F;02&#x2F;25&#x2F;gentips-4-display&#x2F;" rel="nofollow">https:&#x2F;&#x2F;effectivetypescript.com&#x2F;2022&#x2F;02&#x2F;25&#x2F;gentips-4-display...</a>
joshuakgoldbergalmost 3 years ago
TypeScript&#x27;s type system is Turing Complete: meaning it has conditional branching (conditional types) and works with an arbitrary huge amount of memory. As a result, you can use the type system as its own programming language complete with variables, functions, and recursion. This blog post is a starting list of a bunch of the shenanigans TypeScript developers have pushed the type system to be able to do.
评论 #31900007 未加载
评论 #31899725 未加载
tgvalmost 3 years ago
Does anybody have a document that describes all type constructions in typescript? The &quot;official&quot; handbook doesn&#x27;t seem complete. Some time ago, I tried to use tuples in types, but I couldn&#x27;t figure out the correct syntax. I found some vaguely similar examples on stackoverflow, so it seems some people did get that information.
评论 #31905814 未加载
mbrodersenalmost 3 years ago
Advanced type systems are fun to play with. But unfortunately some people get carried away and build a mountain of unnecessary complexity that other developers then have to deal with. A bit like Lisp macros. It is fun to implement your own type system and DSLs in Lisp. But the result is likely to be completely unmaintainable by anybody else <i>and</i> yourself a year later when you have forgotten how it works. I have seen the same happen with templates in C++. Developers that spend weeks having fun building a mountain of template hell to solve a problem that could have been solved in a few hours <i>without</i> template magic. As with everything else, keeping an eye on the benefit&#x2F;cost ratio is key.
评论 #31917644 未加载
truth_seekeralmost 3 years ago
Very very cool. Thanks for sharing. SQL Database engine source code exceeded my expectation.