3 ms·
I'm surprised to see Typescript as a favorite language. I use it every day, and I kind of see it as a necessary evil, but not something I particularly enjoy. I
by LEDThereBeLight 5y ago
I'm surprised to see Typescript as a favorite language. I use it every day, and I kind of see it as a necessary evil, but not something I particularly enjoy. I guess I'm saying this to ask, am I doing something wrong?
I feel like there's essentially no useful type inference, writing custom types is a nightmare because everything has its own special keyword rather than a simple core set of composable types, error messages feel intentionally vague, and having to write constructors and custom "isType" checks for every type you define is a massive chore.
In Haskell I could write:
data Tree a = Leaf | Branch (Tree a) a (Tree a)
tree = Branch (Branch Leaf 1 Leaf) 3 (Branch Leaf 2 Leaf)
whereas in typescript it would be something like:
type Leaf = {
type: 'leaf'
}
type Branch<a> = {
type: 'branch'
v: a
l: Tree<a>
r: Tree<a>
}
type Tree<a> = Leaf | Branch<a>
const isLeaf = (l: any): l is Leaf => l?.type === 'leaf'
const isBranch = <a,>(b: any): b is Branch<a> => b?.type === 'branch'
const isTree = <a,>(t: any): t is Tree<a> => isLeaf(t) || isBranch<a>(t)
const Leaf = (): Leaf => ({ type: 'leaf' })
const Branch = <a,>(v: a, l: Tree<a> = Leaf(), r: Tree<a> = Leaf()): Branch<a> => ({ type: 'branch', v, l, r })
const tree = Branch(2, Branch(1), Branch(3))
How does this not feel like a major pain in the ass? Is there a simpler way to get typescript to do all the boilerplate for you?
- idontwantthis 5y agoDon’t focus so hard on guaranteed type safety, and write more code. For real engineering I think it’s a wonderful compromise between formality and practicality.