Hacker News new | ask | show | jobs
by fishtoaster 1460 days 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:

  import { ValidWords } from "./spellcheck";
  
  // Typechecks cleanly:
  const result: ValidWords<"the quick brown fox."> = "valid";
  
  // Throws a type error
  const result: ValidWords<"the qxick brown fox."> = "valid";

[0] https://github.com/kkuchta/TSpell
5 comments

Your GitHub account is a rabbit hole of crazy interesting "monstrosities" like this. I love them! My favorite ones, apart from this typechecker, are css-only-chat[1] and tabdb[2]

1: https://github.com/kkuchta/css-only-chat 2: https://github.com/kkuchta/tabdb

so that's great and I know this wasn't your point but ....

I use this VSCode extension

https://marketplace.visualstudio.com/items?itemName=streetsi...

To spell check my code. It's surprisingly useful. It doesn't check at compile time but it does check camelCase and snake_case and even in typescript code I've found it highlight various actual issues.

What do you do when a colleague has made the spelling error in the pasta? Open a PR?
This is great!
So does spellcheck contain a gigantic array of the English language?
Yes. It looks like this:

    export type ValidWords<T extends string> = T extends ""
      ? "valid"
      : T extends `the${infer Rest}` | `of${infer Rest}` | `and${infer Rest}` | ...
the source code to answer your question is directly above your question
I'm inclined to cut someone a bit of slack for not following the link to the implementation if the context suggests deeply nested template meta programming golf is involved. It's surprisingly much less complicated than I would have expected.
How long does this take to compile?
Something like 40 seconds for a 9-word sentence. Would not recommend in production. :)