Live data from Hacker News

Extreme explorations of TypeScript's type system

learningtypescript.com

21–30 of 64 posts

Re: Extreme explorations of TypeScript's type system

#21
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 = "valid";
  
  // Throws a type error
  const result: ValidWords = "valid";

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

Re: Extreme explorations of TypeScript's type system

#22
When doing fancy things with typescript types, be really careful - it's possible to accidentally construct typescript types that will increase your tsc compile times by multiple seconds 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 very fancy string literal type.

When this happens, typescript language integration (like in vs code or sublime text) will suddenly fall over and stop working correctly, and it'll be near impossible to figure that out too.

Our build uses rollup to invoke tsc and as it happens their profiling system doesn't actually measure how long tsc takes to run - the time is unaccounted :) So in general, be aware that 'typescript is taking a long time to compile' is a blind spot for this whole ecosystem and if you hit it you're going to have to work hard to fix it.

Re: Extreme explorations of TypeScript's type system

#23

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 = "valid"; // Throws a type error const result: ValidWords = "valid"; [0] https://github.com/kkuchta/TSpell

So does spellcheck contain a gigantic array of the English language?

Re: Extreme explorations of TypeScript's type system

#24

Are there (m)any other languages with type systems as flexible and powerful as Typescript's?

Yes there are. For instance Idris (https://www.idris-lang.org/) has a way more powerful typesystem than Typescript.

If you are looking for more practical and less academic languages, then Scala would be one of the languages that technically has a more powerful/generalized typesystem but at the same time is harder to use compared to Typescript's and cannot do some things that Typescript can do.

Re: Extreme explorations of TypeScript's type system

#25

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 = "valid"; // Throws a type error const result: ValidWords = "valid"; [0] https://github.com/kkuchta/TSpell

So does spellcheck contain a gigantic array of the English language?

the source code to answer your question is directly above your question

Re: Extreme explorations of TypeScript's type system

#26
post #5

To try and limit one'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's quite powerful. TypeScript's type annotations are really a DSL embedded int…

Yes, while that is true, TS errors can sometimes be really clunky. And sans a debugger, it is not uncommon for me to be spending stretches of 20-30 mins almost every week trying to unravel complex type errors that span multiple pages. I recently traced a very weird error to TS changing what keyof never evaluates to in a minor version.

So yeah, using discriminated unions, branded types, mapped types etc. in moderation can substantially reduce the surface area of errors - more so than other mainstream nominally typed languages. However, trying to model and prevent every invalid state at type level can lead to a serious drain in productivity. And, I am not really sure how to draw a line between.

Re: Extreme explorations of TypeScript's type system

#27

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 = "valid"; // Throws a type error const result: ValidWords = "valid"; [0] https://github.com/kkuchta/TSpell

This is great!

Re: Extreme explorations of TypeScript's type system

#29

This is a great list. I feel I'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. I've seen many projects where the typing is done so well that it can infer and include all the data I've fed into the TS-defined functions / classes, which is great for IDE autocompletion.

What are some of the projects have done typing that well?

purify-ts is my favourite currently.

Re: Extreme explorations of TypeScript's type system

#30

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 = "valid"; // Throws a type error const result: ValidWords = "valid"; [0] https://github.com/kkuchta/TSpell

So does spellcheck contain a gigantic array of the English language?

Yes. It looks like this:

    export type ValidWords = T extends ""
      ? "valid"
      : T extends `the${infer Rest}` | `of${infer Rest}` | `and${infer Rest}` | ...
Post reply on HN