Live data from Hacker News

Functional Programming with TypeScript's Type System

desislav.dev

41–46 of 46 posts

Re: Functional Programming with TypeScript's Type System

#41

Earlier quoted context omitted.

It’s good for reference but not for discovery. If I already know the general concept (let’s say Template Literal Types) I can get good info on it, but if I start with a question like ‘Is there a way to make sure this string literal starts with “id_”?’ then I find it very hard to know. Random but this is what I’m finding GPT-4 best at: translating random questions into domain terminology + providing examples.

maybe this is just an issue with me but I've not found any way to search the docs. the only thing Google seems to index is the release notes, and going backwards from the release notes to guess the appropriate section of the docs that will explain that concept is really annoying.

I can’t even find basic type definitions for standard JS functions. I have to type the function in my IDE and then open the type definition from there.

So it works, I guess, but that’s not really how I want to work.

Re: Functional Programming with TypeScript's Type System

#42

Earlier quoted context omitted.

Why was the `dogs` array initialized as an `Animal[]` type instead of `Dog[]` type which would forbid the addition of a `Cat` type? Why would you be able to map a call to `woof` over an `Animal[]` when `Animal` doesn't implement `woof`? I don't understand how the SO link answers these questions.

> Why was the `dogs` array initialized as an `Animal[]` type instead of `Dog[]` That might be your confusion: it wasn't. Its type is `Dog[]`. > which would forbid the addition of a `Cat` type? Why would that be forbidden? The problematic method is `append_animals`, which only cares that both arguments satisfy `Animal`, which both `Dog` and `Cat` do. > Why would you be able to map a call to `woof` over an `Animal[]` w…

I see what you're saying. Thanks for taking the time to explain.

Re: Functional Programming with TypeScript's Type System

#43

At this point I'm wondering if the TypeScript type system can be used for dependant types that would allow formal verification of the programs

That'd require it to be sound and programs to be total: an infinite loop is a proof of anything, and type system unsoundness leads to false proofs

Re: Functional Programming with TypeScript's Type System

#44
Is it just me or do other people read things like "covariance on a mutable generic type" and just want to get stuff done? Maybe it's because nowadays I do solo or small team projects but this is why I fled to Elixir, it's mostly dynamically typed and you can gradually get into types when you want it, Elixir is cool with that.

Re: Functional Programming with TypeScript's Type System

#45
post #26

As wonderfully absurd as this is, I learned more about TypeScript’s type system from this post than I have from its documentation. Entirely possible that PEBKAC, but I’ve found TypeScript’s documentation to be on the worse end of the programming language documentation quality spectrum.

It's not just you. To learn how to express more advanced types (or learn whether they are even possible to express), I've had to Google, read source code, or scan random medium articles and blogs from tech companies. Rarely have I learned anything new from the TS docs.

I learned more from this type definition than anything I’ve ever read about TypeScript’s type system before:

  interface add extends F { out: this['args'] extends   [infer a, infer b]     ?
  a extends Zero         ? b :
  a extends Suc ? Suc> : 

  never : never
}

Re: Functional Programming with TypeScript's Type System

#46
Can you? Certainly. Should you? Ehhhhh…

The post is experimenting with the type system—which is neat—but before you think you should push this in production (having been in positions to work with heavy-FP’d code), consider an actual FP language if that’s the style you want. The ergonomics are so bad compared to any FP lang→JS option. Currying/partial application, first-class composition/bind/apply, pattern matching not using a ._tag property with a String key, and more are just missing (see: migrating from PureScript/Haskell[0] for fp-ts to see how verbose basic concepts becomes). The other issue is that with TypeScript being multiparadigm and idiomatic TS leaning to imperative/OO due to ergonomics and Java’s legacy on culture/education, there’s a good chance your coworkers/contributors will be expecting a non-FP style which will cause even more friction as you try to justify ‘purity’ to folks that just see a verbose codebase that likely is leaning hard into a lib, quality as it is, like fp-ts which cosplays as Haskell, PureScript, OCaml, Idris, et. al.

[0]: https://gcanti.github.io/fp-ts/guides/purescript.html

Post reply on HN