Live data from Hacker News

When to Use TypeScript – A Detailed Guide Through Common Scenarios

khalilstemmler.com

221–230 of 244 posts

Re: When to Use TypeScript – A Detailed Guide Through Common Scenarios

#221

Earlier quoted context omitted.

> design your system first just by writing the types, Fine. > look how it works, Beutiful except it doesn't work yet because it's just types that make some shape. You could literally draw the thing with pen and paper. > go bother with implementation. And only then discover that something you have thought of as implementation detail is one of the main problems with your solution. No problem. I'll just refactor some ty…

> You could literally draw the thing with pen and paper. Paper does not tell you when something you draw is wrong. > And only then discover that something you have thought of as implementation detail is one of the main problems with your solution. What is no different from learning that something you implemented is wrong, except in that you spent less time to get the problem. Or do you expect some magical designing t…

> Paper does not tell you when something you draw is wrong.

With some things it does. At least better than your brain does. Typesystem does it even a bit better. But you end up with a picture not a program nonetheless, before you start writing implementation.

> What is no different from learning that something you implemented is wrong [...]

What is different is that with all the types you wrote, the process of problem discovery that happens during implementantion is stifled by what you wrote so far. Also you have additional temptation to contort your types and to leave it in a bad state because you wrote so much already and you'd very much want to avoid tossing that out.

My point is that you discover problem through experimentation and types make experimentation harder because they make the code in some ways harder to change. TypeScript avoids that by allowing you to use as much types as you want at any stage of problem/solution discovery but not forcing you to.

Re: When to Use TypeScript – A Detailed Guide Through Common Scenarios

#222
On the other hand. If your code base has become so big that you need something like TypeScript to make your life bearable, you're probably doing it wrong.

Having strongly typed languages has much more to do with those developers who can't live without the autocomplete feature in their IDE. Especially the army of ASP.NET developers who are used to working with Visual Studio.

We'll probably be seeing more of those code generator patterns with TypeScript soon.

Not saying TypeScript is a bad thing. But strongly typed languages do come with their pitfalls. Massive amounts of code providing a simple CRUD interface. An entire team working on their complex microservice solution which is just wrapping some already existing API. Things that can easily be achieved with a couple of lines of cough PHP cough.

Re: When to Use TypeScript – A Detailed Guide Through Common Scenarios

#224

The article mentions object-oriented programming several times as a helpful paradigm (especially for domain-constrained problems where DDD is helpful). I’d also like to point out that functional programming is tremendously helpful for solving these types of problems, especially when combined with use of modules. TypeScript is absolutely capable of modeling, checking, and otherwise handling types in a functional progr…

I really hope I don't come off sounding needlessly contrarian, but I've had the complete opposite experience when it comes to TypeScript and functional programming. I posted about the specific issues I ran into here, and they seem to be pretty fundamental w.r.t the ability of TypeScript's type inference to work with function composition in general: https://news.ycombinator.com/item?id=19600475 But the fact that you (…

> But the fact that you (and presumably plenty of others) seem to be having a great time with functional programming in TypeScript leads me to believe that there might be something I'm missing

I've noticed "functional programming" turned into an umbrella term.

For some people it means Haskell: strongly typed lazy evaluation with a focus on ADTs and functions that operate on them (i.e. methods) to achieve composition. Heavy use of pattern matching.

For others it means Clojure: dynamic language that favors raw duck-typed data manipulation. Heavy use of macros.

Then there's the pointfree crowd: very little use of literal functions, most of them are just composition of curried/partially-applied primitives.

For many it just means "I use map/filter/reduce".

And for most, it's just a mix-and-match of subsets of all those.

My experience matches yours: Typescript is mostly fine with map-filter-reduce and that's it. I'd love to be proved wrong though.

Re: When to Use TypeScript – A Detailed Guide Through Common Scenarios

#225

Earlier quoted context omitted.

I don't know if what you've described is an _opposite_ experience. I think TypeScript would work fine for me too if I never needed anything compilation-wise outside of what tsc is already capable of. But as soon as you do, the lack of extensibility of tsc compared to babel becomes painfully obvious.

I think it is the opposite. Even without TypeScript Babel has grown to be overly complex. If you’re talking about front end libraries like React you’re better off ditching Babel altogether and going with something like Parcel. It just works. The TS developer experience ya sim proved working with JS enormously for me when I have to do it, and nudged me toward simpler, smarter development environment setups. I can’t sp…

Babel's designed first and foremost with extensibility in mind. Of course an opinionated build tool with no extensibility to speak of (tsc), is going to be easier to set up and get started with than one that designed with extensibility as a core principle.

That doesn't mean one's necessarily fundamentally better or worse than the other, but the great thing about extensible tools is that they can often be used as building blocks for other more opinionated tools that abstracts away configuration details from the user by providing an opinionated set of defaults, and still offer the ability to customize when users' needs are not adequately met by the defaults.

Case in point - Parcel actually uses babel under the hood:

https://parceljs.org/javascript.html#default-babel-transform...

All it does is configures a default set of plugins & presets for you. You can override any and all compilation behavior Parcel provides by default simply by adding the appropriate babel config files.

TypeScript could have done the same with their build tooling, by extending babel with plugins/presets and providing the exact same set of functionality to users as what tsc's is capable of currently in terms of compilation features (which is currently afaik a strict subset of what babel + its plugin ecosystem is able to provide).

That would give users the exact same out-of-the-box experience as tsc does currently, and offer them the opportunity to extend compilation behavior with additional plugins and presets when the need arises.

That extending the compilation behavior of tsc, after so many years of development and wide adoption, is still not even on the roadmap right now is the core of my disappointment with them choosing to develop their own _non-extensible_ compilation tooling vs just extending babel, where they would have been able to offer extensibility for free from day 1.

Re: When to Use TypeScript – A Detailed Guide Through Common Scenarios

#226
post #34

It's a default for me now, on every new project. It doesn't slow down development because TypeScript allows you to easily "step down" type constraints as desired, and writing nontrivial code without any kind of type hints is just unimaginable to me now.

> It doesn't slow down development I don’t know about the rest of the world, but I’ve never once been slowed down by a compiler saying “It won’t work like that. Don’t waste your time”.

Hi, I'm from the rest of the world. I have successfully made small changes to a program which basically broke it, just to do a proof of some concept. I was able to run the thing and test the concept, while not stepping on broken things.

I don't want to commit to refactoring a large amount of code for the sake of a small change, just so that some code-writing robot gives me permission to run the whole thing.

Re: When to Use TypeScript – A Detailed Guide Through Common Scenarios

#227

Earlier quoted context omitted.

> It doesn't slow down development I don’t know about the rest of the world, but I’ve never once been slowed down by a compiler saying “It won’t work like that. Don’t waste your time”.

> > It doesn't slow down development > I don’t know about the rest of the world, but I’ve never once been slowed down by a compiler saying “It won’t work like that. Don’t waste your time”. I'm right there with you, I really don't understand this train of thought that catching errors faster somehow makes you slower. I like having the compiler there to catch my errors when I make them rather than some number of minutes…

Your remarks apply only to cranking out new code. Static typing prohibits making fundamental changes to an existing body of code, like changing a core, widely-used data structure. To make such a change, you have to be extremely sure that you want it and that you're doing it right. Then you can commit to the days and weeks of refactoring at the end of which you once again have a program you can run.

Under dynamic typing, you just do a small part of the job, yet the program builds; you can get in there and try things. Decisions about how to proceed can be guided by errors you run into when you step on the things broken by the partial refactoring. Seeing things working right away can be a motivator to get the whole thing done. Or it can help you see that, oops, the change is not worth doing or bad for whatever reason: you dodged a bullet with a small amount of sunk cost (the few changes you made that you can throw away without a whole lot of regret).

Static typing helps with small refactoring where the ripple effect is small. The compiler pinpoins things that have to change; they happen to be few in number, and don't have too many ripple effects of their own. If your change breaks everything, then it's moot; the compiler tells you there are breaking changes everywhere.

There is another angle: under dynamic typing, changes can be done in ways that things continue to work. You can write modules that have "absolute genericity": they work with objects of any type without being recompiled. Such things are bullet-proof against all conceivable refactoring.

Re: When to Use TypeScript – A Detailed Guide Through Common Scenarios

#228

Earlier quoted context omitted.

Yep I feel the same. TypeScript improves my workflow tremendously and I use it for everything now. Going back to regular javascript feels like driving without a seatbelt. Sure you can but why? Type-checking and smarter autocompletion mean I don't make mistakes as often and my productivity is higher. (No longer run the app, see I misspelled a function call, fix and run again, etc.) I also feel unit-level tests are les…

> I also feel unit-level tests are less necessary with TypeScript. Especially with strict compiler settings, including strict null checks, and the good type conventions they encourage. To give a small example, if you have a value that can either contain an error or a successful result, you can model it as the union of a success and error state. This prevents you from accessing the value until you check the result is…

I'm using TS for front/back end but you do need runtime checking as well at a few places, even though the compiler tells you it's redundant.

Relying strictly on typing is dangerous as you might've given wrong type from data coming back from database queries and you can't tell until it has run.

Re: When to Use TypeScript – A Detailed Guide Through Common Scenarios

#229
post #34

It's a default for me now, on every new project. It doesn't slow down development because TypeScript allows you to easily "step down" type constraints as desired, and writing nontrivial code without any kind of type hints is just unimaginable to me now.

Wait, type checking is good again? Suits me. Welcome to 1985 folks :-)

Don't like pedantic attitude just because some recent paradigm matches that of a while ago. Back then, you don't know dynamic types won't be too welcomed.

Re: When to Use TypeScript – A Detailed Guide Through Common Scenarios

#230
post #27

Typescript is wonderful, especially as someone used to writing a lot of java/groovy. Typescript is like pouring cement around your javascript house of cards. It makes writing front end code painless and predictable. It also has amazing tooling in intelli-j. Code completion, linting, package recognition, all the good stuff.

> It also has amazing tooling in intelli-j. Code completion, linting, package recognition, all the good stuff. ... and in the free Visual Studio Code, also crossplatform like IntelliJ. (Nothing against IntelliJ, JetBrains is a cool company with amazing products IMO, I just prefer VSCode myself.)

I think VS Code is almost like a little brother of IntelliJ now but better at a few places. (Especially rendering.)
Post reply on HN