by quickthrower2 · 7y ago · 0 comments
Otherwise you hope that someone has written in a doc somewhere that bar is a string. And judging by npm docs I've seen - that is very unlikely. So you debug at runtime, head over to the github repo, or do an console.log(typeof(... hmm!
My experience with TS has been great until I need to deal with data that comes from an external system (API, client, database). Then I either have to cast the type (which can lead to bugs where compile and run time types don't match and IMO isn't an option server-side) or I have to effectively duplicate my types by writing type guard functions.
>Change a front-end React prop, then follow static type errors through the statically typed API, into the backend, all the way down into the database where they force a change in the Postgres schema. The system won't even compile unless React can talk all the way down to Postgres.
https://twitter.com/garybernhardt/status/1140695491685933060
The success here lied in using various tools, applied pragmatically at each step according to the given context. Probably the most important tool is defensive coding and building up a system from decoupled, almost visibly-correct components. Then some nice technical tools thrown in: dynamic development with hot loading, some modest storybooking of components, a repl, good backend libraries, conscious and measured unit testing, strong build/deploy/release tools.
The ability to turn on a dime and incrementally refactor things was paramount to the success and efficiency of this development.
Then someone says we need to use strong static typing. All the way. No judicious application of this tool, we'll use it everywhere. We'll make everything cohere to the type system. This is no pragmatic choice. This is religion. You know its religion because its an ideology that pervades the whole system. There is no judicious application. The extreme tax of contending with types as your system evolves is high. I've only ever seen strong typing enthusiasts deny this truth. Will one enthusiast be honest here? You can literally stand over a strong typing enthusiasts shoulder as they spend an hour running all over their system adjusting their types for one minor change in business requirements and they'll still say, "No, no, types don't cost anything..."
That said, even if I have to write type guards at the edges (which I'd do in JS anyway, just because you aren't using types doesn't mean you don't need to enforce schemas), I think being able to rely on compiler typechecking inside the codebase boundary is really nice for making module interactions more predictable.
There are some really great things about types as documentation, though:
1) It's machine-checked, so it's probably not out of date or mistaken, which can be worse than no documentation.
2) The interaction with my code is checked, so if I misunderstood the compiler often has my back.
3) With inference, I can often ask for docs for code I just wrote.
None of this is to say that there aren't usually other forms of documentation that should be produced.
(a -> b -> c) -> b -> a -> c
1. Call the function 2. error 3. infinite recursion
Technically correct - there are infinite possible error messages.
For one.
Now tell me: are types the best documentation?
f :: (a -> b -> c) -> b -> a -> c
result in f (+) 1 2
evaluating to "cookie"
? For one, the types (obviously, to me at least) don't work out.You're pattern matching on a number and returning a string. The function might be handed a function and expected to return a file handle for all you know from inside the function.