166 points · by behnamoh · 3y ago · 124 comments
Did anyone ever think "Hang on, maybe the problem, the reason why coding is so difficult is that interfaces are often too darn complicated to keep track of? Oh, wait a minute, if the interfaces were really simple, would we even need TypeScript at all? Why don't we just design our software in such a way that the interfaces between components are simple so that we can easily remember which functions accept what kinds of input without losing our train of thought? Wait a minute, this idea already exists? It's called loose coupling and high cohesion. How in Alan Kay's name did I not think of it before???"
TS is complicated and chaotic because the language it compiles down to is. TS' aim is to model the behaviour seen in JS in a flexible, user-friendly type system. Good luck!
What doesn't make sense to me is when teams (like mine) have a blank slate and the choice of any mainstream backend language, and then opt in to that ecosystem anyway. TS is good when you're coming from JS sure, but some of us have used more "sane" languages like C# and Go :(
Not exactly sane. Go is one of the most clunkiest awkward languages I've ever worked with. The only benefit is that it's somewhat easy to learn.
It's like a modernised C but for web/infrastructure. Unfortunately ignores about 3 decades worth of progress in other languages, and is simplistic to a fault. Jumping into an unknown codebase is easier than every other language I've used as a result.
I would have gone with C# or Kotlin if I had the luxury of choice, but I'll get that with a deliberate career move maybe.
I hear this regularly but don't think it holds water. Other statically typed languages compile to any number of targets without issue; machine code, llvm, and even JS.
I suspect what you mean is that they aren't just compiling down to JavaScript, but they are wanting to do so in a way that there is no foreign function interface (FFI) consideration for folks in either language. That is, largely to say, they don't want a Typescript runtime?
> 3. Impose no runtime overhead on emitted programs. > 4. Emit clean, idiomatic, recognizable JavaScript code. > 7. Preserve runtime behavior of all JavaScript code. > 8. Use a consistent, fully erasable, structural type system.
https://github.com/Microsoft/TypeScript/wiki/TypeScript-Desi...
This is sarcasm, right? The creator of TS is the lead architect of C#
C# is in some ways less shackled by the runtime it has to target, while TS compiles away entirely. The CLR can be changed to suit .net/C# needs, and JS is not under their control.
If you want simple interfaces, just write simple interfaces. TypeScript does not prevent you from that. But it makes complex things possible, when you need it.
If you work with developers who like to make things needlessly complex, the solution is not to make the tools less powerful.
>> but now many developers are actually using it as such on real projects
Seriously? That sounds like being bored and overpaid and/or not having enough pressure to make actual web and implement new features.
Type annotation work is definitely the annoying part which slows me down and breaks my train of thought.
That said, I don't dislike TypeScript in theory as a language if used carefully. It may just be worth the annotation effort (kind of like how comments are usually worth the time they take to write). I just don't think it's worth the additional transpilation step and all the compatibility and source mapping issues that come with it.
This is akin to throwing water over an oil pan fire, then being frustrated at the water for your house burning down.
You need to either use the Typescript way throughout your dev process, or not use Typescript. Both of those options are valid, but you're currently doing half of both, resulting in unnecessary frustrations.
So maybe, TS working as intended
Also depends on your TS config probably, but TS does a pretty good job with inference, so I find that I don't need to write that many type annotations, especially when assigning variables
Haven't brought this up in a while. Ya. I feel coding is hard due to complex interfaces. I once worked on a programming abstraction, mechanisms, aiming for a consistent interface for easy composition.
Key features of mechanisms were:
1) All data types were mechanisms
2) A Mechanism needed no context (parameters) when invoked.
3) Composition of a mechanism could take any number of mechanisms (primitives at this point are treated as mechanisms) and return a mechanism.
4) Upon invocation, a mechanism functions in one or more modes, returning either a primitive data type or another mechanism.
Think of it as currying or Lisp's S-expressions, but supercharged, envisioned as a base for Visual Programming Languages.
An example of composition using mechansisms:
// Compose
addTwo = print( map( add( 2, emitFromRange(0, 20, 4) ) ) );
Which is saying "Print a mapping of adding 2 to an emitted range from 0 to 20 by 4."
// Invoke
addTwo()
For those of a less elevated intellect, I think the answer is that becoming Turing Complete is actually a very low bar. Once you've got recursion and pattern matching I'm pretty sure it just drops out in the wash. "Type checking is unification" (some computer scientist or other, possibly Luca Cardelli)
Good luck in defining 'simple'. For me, a simple function only accepts inputs which it knows how to handle, and performs no side effects. Bonus points if it always terminates. Personally I cannot maintain this level of simplicity without a type checker.
But on the plus side, you're still allowed to remember what your functions accept! The checker only kicks in if you get it wrong.
And then everyone replies with, “well that’s not _real_ OO - if you did OO right the software would be perfect!”
Except real OO cannot be done.
So, yea types are a tool and should be used wisely. But let’s not pretend like there’s some magical methodology out there that removes complexity from software.
Ironically the separation of state away from logic is why backend web development tends to be easier than say game development.
If you are using typescript you can either go down the path of using `as` and `any`, which at that point the benefits start declining. Or you can work really hard with typescript and make something complicated that correctly matches your code's logic.
But saying, "write simple code" doesn't really help much without saying how to do it.
TS is lauded, but I really think it's just the best of a bad bunch.
But at the time, many complained of the lack of jQuery support and needing to transpile to support other browsers, so it didn't get the interest it deserved.
If you're writing software and you're consuming libs and frameworks, a fragmented ecosystem sounds less than ideal.
I get that typescript's decision was to model js's behavior exactly in the type system. But now having seen the costs of that and how much can be gained by relatively small constraints on js I think it was a mistake.
- Brian Kernighan
I agree with the notion that simplicity is hard though.
This is my "lodash for types" project. It supports integer division for up to 2^64, for example. It has over 200 composable utility types.
lodash does come to mind in fact. The last few shops where I've worked use a very small subset of lodash utilities and ignore most of the library. Their documentation is terse so it's often easier to just write your own code that is longer but more readable to the average engineer.
These projects are interesting in an amusing "oh look, another Turing complete type system" way, for the author to gain expertise, and as something to highlight in CVs and to chat about during interviews. But other than that, I would never actually choose to use any of this in a real-world project. Depending on the stability of this API (TPI?), and subjecting colleagues to esoteric libraries would be a nightmare to maintain and support.
This can be frustrating in the moment sometimes, but other times a type can do two really powerful things:
1. prevent someone you’ve never spoken to from doing something legitimately dangerous 2. provide browsable documentation about the constraint its enforcing and how other places in the code resolve the constraint.
It doesn’t always work out that way, sure, but that’s the ideal types are striving for.
In cases where the type system can encode the invariants you care about, it can be worth the extra complexity so that you have code that has compiler enforced correctness (at least in some aspects). Of course, you can't do this for any invariant in a given type system (eg lifetimes are not representable in Java). When it is possible though, debugging a type system has the advantage that it's done on your local machine, away from prod.
It would be funny to see web servers running the typescript compiler (type checker) instead of a JS runtime, though.
Eventually we need to make type systems just metaprogramming using the primary language that they're intended for. There's no reason for those to be two languages.
Nim is like this and its fantastic. None of the weird special rules for metaprogramming, just Nim code manipulating ASTs at compile time procedurally. Can even use standard library.
I'd love to know if anyone could reproduce the N-queens example in Nim: https://www.richard-towers.com/2023/03/11/typescripting-the-...
I believe it is possible, but don't have the time to try it out.
> The Nim compiler includes a simple linear equation solver, allowing it to infer static params in some situations where integer arithmetic is involved.
From: https://nim-lang.org/docs/manual_experimental.html#concepts-...
The issue with meta programming is that it’s too powerful, in the sense that the transformations can’t be statically checked for typing rules.
It doesn't matter how flexible the macro programming is, if it reduces statically to something you can typecheck, therefore this artificial segregation of syntax and rules (and mental models) represents us solving a problem superficially, in an almost cargo-cult way, because we never stopped long enough to think about at depth.
Probably stupid question, but that probably means that one could, theoretically, implement a type system for any one of those DSL(-like) type systems, isn't that correct? And so on and so forth, we could have, theoretically, DSL-like type systems all the way down.
The most generic one I know of is Idris. It uses some heuristics to make some functions decidable that in theory should not be.
It doesn't have to be that way and in my opinion for general purpose languages it shouldn't. The type system should be about constraints that facilitate reasoning about your code - reasoning by machine and human.
Compile time computation should be a separate facility.
(That a lot of that was to facilitate reasoning about nearly untyped JS code doing a lot of dynamic code things is, depending on which side of the deliberation you are on: 1] a sign that dynamic code has always been that complex and developers have had to do all that sort of "compile time logic" in their own heads for so long, and/or 2] a sign that Typescript's "flaws" come from trying to be too supportive of existing bad JS code.)
Most of them can be coerced to do general computation, especially if they have things like if-branch, and some sort of self-reference.
But this is only true for certain languages. Overall most type systems are not Turing complete and therefore not suited for general computation
https://www.richard-towers.com/2023/03/11/typescripting-the-...
(Edit: this is mentioned elsewhere in this thread by Kerrick)
Zig strives to use the same syntax for metacomputation, although its approach is not tightly integrated into the type system, which is why you'd see compilation errors at expansion, not function definition.
My goal was to pass the Typescript types to the JS runtime, maybe there's a better way and GPT-4 just led me astray...
[0] https://github.com/itsdouges/typescript-transformer-handbook
https://github.com/ronami/meta-typing/blob/master/src/utils/...
e.g. :
type zero = []
type Inc<N extends string[]> = [...N,'']
type Dec<N extends string[]> = N extends [...infer T,''] ? T : never
type one = Inc<zero>
type two = Inc<one>
type oneb = Dec<two>Another approach which I tried is to do arithmetics on digits directly, storing digits in a tuple instead, but the code is not as elegant as the tuple one
https://github.com/dqbd/ts-math-evaluate/blob/main/src/math/...
Once we annihilate all human life on earth, the stock market will be at $0.