Live data from Hacker News

Extreme explorations of TypeScript's type system

learningtypescript.com

41–50 of 64 posts

Re: Extreme explorations of TypeScript's type system

#41

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

How long does this take to compile?

Something like 40 seconds for a 9-word sentence. Would not recommend in production. :)

Re: Extreme explorations of TypeScript's type system

#42
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…

It isn’t unusual that a complex working solution can be simplified over time. Isn’t that part of eliminating tech debt or something?

Typescript is just one of those languages where things can get carried away but then reigned in again with a nice complexity gradient.

Re: Extreme explorations of TypeScript's type system

#43
post #38

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 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?

Re: Extreme explorations of TypeScript's type system

#44
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.

Example of the code: https://github.com/pj/typeshaman/blob/main/packages/graphql/...

Documentation is incomplete, unfortunately I had to get a job. I started working on encoding all of SQL as well.

Re: Extreme explorations of TypeScript's type system

#45

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

[deleted]

Re: Extreme explorations of TypeScript's type system

#46

Earlier quoted context omitted.

Hey Mark, I’m actually currently looking at a similar problem. I’m writing a HTTP client based on composition. The exact details aren’t important, but one of the goals is to have a strong type system for describing a valid pipeline of things like response parsers. Imagine something like Doing “type tests” alone isn’t too hard - we can just use conditional types and the extends keyword. If the code compiles, fine. But…

I use @ts-expect-error for testing the negative case, but if there was a practical way of testing the actual error reported that would be wonderful.

My solution to this was to run tsc from Jest tests and do snapshot tests for the error messages

https://github.com/noppa/get-optional/tree/master/tests/typi...

Re: Extreme explorations of TypeScript's type system

#47
post #39
post #32

Earlier quoted context omitted.

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 think I disagree (though happy to be corrected). It's common to have dervied types (example Base = Shape, Derived = Circle, Rectangle, Hexagon). Often you have a collection of Shape and often there is some runtime code that can go from a Base type to a Dervied type based on some key or value but the relationship between the key and the Dervied type is rarely directly expressed in the type system where as in typescr…

Keeping in mind that it can only associate compile-time-known values, there's no practical advantage over something like a tagged union.

Re: Extreme explorations of TypeScript's type system

#48

Earlier quoted context omitted.

We do a _lot_ of this in the Redux library repos (examples: [0] [1] [2] ). We have some incredibly complicated types in our libraries, and we have a bunch of type tests to confirm expected behavior. Generally, these can just be some TS files that get compiled with `tsc`, but it helps to have a bunch of type-level assertions about expected types. I actually recently gave a talk on "Lessons Learned Maintaining TS Libra…

Hey Mark, I’m actually currently looking at a similar problem. I’m writing a HTTP client based on composition. The exact details aren’t important, but one of the goals is to have a strong type system for describing a valid pipeline of things like response parsers. Imagine something like Doing “type tests” alone isn’t too hard - we can just use conditional types and the extends keyword. If the code compiles, fine. But…

Hmm. While it may not be the immediate answer to your question, my Redux teammate Lenz Weber ( @phryneas ) wrote a Remark plugin that parses TS codeblocks out of Markdown and actually runs them through the TS compiler. As part of that I know he generates a bunch of "virtual files", including some parsing that lets you add an extra section of the codeblock representing another file to be compiled along with the actual example. The source for that may at least help give you some examples of how to use TS programmatically:

https://github.com/phryneas/remark-typescript-tools

Re: Extreme explorations of TypeScript's type system

#49

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 so…

This doesn't even require a type system anywhere near as fancy as what TS has. C#, for example, has a similar problem with how type inference in lambdas interacts with overload resolution, once you start nesting those lambdas:

https://docs.microsoft.com/en-us/archive/blogs/ericlippert/l...

Re: Extreme explorations of TypeScript's type system

#50
These are some of my "Typescript type system" based projects:

Wordle: https://codesandbox.io/s/wordle-typescript-d4srgi?file=/src/...

Anadrome(Anagram Palindrome): https://codesandbox.io/s/anagram-palindrome-7u14xr?file=/src...

Candy Crush 1D: https://codesandbox.io/s/candycrush-u2v5pr?file=/src/index.t...

Post reply on HN