Live data from Hacker News

Extreme explorations of TypeScript's type system

learningtypescript.com

51–60 of 64 posts

Re: Extreme explorations of TypeScript's type system

#51
post #32

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

I believe that typescript type system is so flexible, powerful and complex just because it had to be adapted and built around the shortcomings and limitation of javascript. It makes no sense to have something like it if you build a language from the ground up (or if you could just scrap backward compatibility in a bad designed one)

I've had to work with some really complex data types and I have really appreciated the utility types, conditional types, modifying data type recursively, template literal types etc. I was wondering how many other types languages are able to do all that stuff.

Re: Extreme explorations of TypeScript's type system

#52

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.

I really like the intro page for Idris.

> In type-driven development, types are tools for constructing programs. We treat the type as the plan for a program, and use the compiler and type checker as our assistant, guiding us to a complete program that satisfies the type. The more expressive the type is that we give up front, the more confidence we can have that the resulting program will be correct.

Great stuff.

Re: Extreme explorations of TypeScript's type system

#53

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

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

Re: Extreme explorations of TypeScript's type system

#54
post #33

> 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. Also, as you start getting complicated logic in your types, you need to test your…

I lost it when at my previous job I found a 20 multiline super complex type defined by another dev, asked him to describe it because I was in a tight deadline and had not time to parse whatever he was defining. He starts with "it's pretty simple" and then used like 10 minutes to describe me what he meant while writing on paper the various pieces getting confused two times. At the end of the day it could be simplified…

>I lost it when at my previous job I found a 20 multiline super complex type defined by another dev, asked him to describe it because I was in a tight deadline and had not time to parse whatever he was defining. He starts with "it's pretty simple" and then used like 10 minutes to describe me what he meant while writing on paper the various pieces getting confused two time

sorry about that

Re: Extreme explorations of TypeScript's type system

#55
post #32

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

I believe that typescript type system is so flexible, powerful and complex just because it had to be adapted and built around the shortcomings and limitation of javascript. It makes no sense to have something like it if you build a language from the ground up (or if you could just scrap backward compatibility in a bad designed one)

No, TypeScript has these type system features because they are necessary to accurately type the programs people actually write using JavaScript.

This isn't because of the shortcomings of JavaScript, it's because of the shortcomings of simple type systems.

Simple type systems (like those of Java or Go) restrict the kinds of programs you can easily write while satisfying the type checker. Normally, the alternative is resorting to languages without static type checkers, like JavaScript.

TypeScript allows mostly idiomatic JavaScript even with complex runtime invariants to be given quite accurate types at compile time. It has features I miss all the time in Java.

Re: Extreme explorations of TypeScript's type system

#56
Does anybody have a document that describes all type constructions in typescript? The "official" handbook doesn't seem complete. Some time ago, I tried to use tuples in types, but I couldn't figure out the correct syntax. I found some vaguely similar examples on stackoverflow, so it seems some people did get that information.

Re: Extreme explorations of TypeScript's type system

#57
post #56

Does anybody have a document that describes all type constructions in typescript? The "official" handbook doesn't seem complete. Some time ago, I tried to use tuples in types, but I couldn't figure out the correct syntax. I found some vaguely similar examples on stackoverflow, so it seems some people did get that information.

It helps to read the release notes to keep up to date. https://www.typescriptlang.org/docs/handbook/release-notes/t...

Re: Extreme explorations of TypeScript's type system

#59
post #57
post #56

Does anybody have a document that describes all type constructions in typescript? The "official" handbook doesn't seem complete. Some time ago, I tried to use tuples in types, but I couldn't figure out the correct syntax. I found some vaguely similar examples on stackoverflow, so it seems some people did get that information.

It helps to read the release notes to keep up to date. https://www.typescriptlang.org/docs/handbook/release-notes/t...

Thanks. Looks like there are a few release notes to catch up.

Re: Extreme explorations of TypeScript's type system

#60
If you're going down the rabbit hole of writing complex types, check out Dan Vanderkam's "The Display of Types" 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!

[0]: https://effectivetypescript.com/2022/02/25/gentips-4-display...

Post reply on HN