r/javascript • u/DanielRosenwasser TypeScript • Oct 01 '22
Ten Years of TypeScript
https://devblogs.microsoft.com/typescript/ten-years-of-typescript/11
u/EarthMandy Oct 02 '22
Still remember that fun transition from JS to TS at a start-up I was at, when we converted our first common library by setting every type to any and giving ourselves a pat on the back. Learnt a few things since then.
12
u/robpalme Oct 01 '22
The linked first blog post is also worth a read. It's amazing how consistent it is with how things worked out 10 years later.
4
Oct 02 '22
Are most of the die-hard typescript supporters working in nodejs or front end frameworks?
8
Oct 02 '22
Full stack, having type safety between the backend and front end is one of best aspects.
1
u/abrandis Oct 02 '22
This is one of the.biggest misconceptions I hear about using the same language in Frontend and Backend ,if you've seen enough projects you realize that you really don't share that much core code between them.. sure for common things like data validation or string manipulation you might find some commonalities but not much else...
I still don't understand the value of NodeJs in the backend a single threaded language for a server? Why? Isnt threads or equivalents the whole reason Google , who knows a thing or two about large scale server processing..created Go for server side processes...
6
u/CyberpunkCookbook Oct 02 '22
In my understanding, the argument is more for developer productivity. Devs can switch between front and back work with minimal friction.
I’m sure Go is more efficient in terms of computing resources, but few companies operate at the scale of Google to where they’d see a significant cost saving.
3
u/30thnight Oct 02 '22
type safety between the backend and front end
Choice of backend language isn't really relevant here to be honest.
If the backend exposes a typed Swagger / OpenApi compatible API, you can generate types to be consumed by your Typescript front-end project.
Having types for front-end projects at all is an incredible DX improvement.
2
Oct 02 '22 edited Oct 02 '22
In general single process is not terrible for async backends largely because most web backend are not doing anything particularly complicated (largely waiting for network/database IO). My 2016 laptop could handle 100-500 req/sec per process at reasonable response times depending on what i was doing.
As for validation, models, and such not shared, not really true. Validation is easy to share, models a bit more complicated but can be shared if structured correctly to have server vs client subtypes
Those who are doing computationally expensive tasks in app often rely on queues (havea separate app cluster do the heavy lifting), threading (workers within that single process) or clustering (multiprocess typically handled by systemd or pm2 round robin-ing requests).
Go was written for entirely different reasons than you present.
1
u/abrandis Oct 02 '22
The issue is why do we use async? That's just syntactic sugar around single thread process, it's clunky mechanism and it doesn't really solve anything if you have long running processes on the server and you need to add more layers (mq) to respond when the processing is done..
Go and other dedicated server processes are built around handling this type of requests much more cleanly.
NodeJS was really more of a proof of concept that Ryan Dahl wanted to see what taking the V8 JS engine out of the browser and making it a standalone process to better handle simpler message queue type requests. It wasn't designed as a true serve reprocess , from there it became popular, but it wasn't built from the ground up for the task it was re-purposed
3
Oct 02 '22
False. Async is not syntactic sugar around single thread process. It is syntactic sugar for Promises, which itself levies the JavaScript event loop. If anything in a non-cpu starving work load, it’s fairly efficient compared to spinning up multiple processes or incurring costs context switching between threads.
It also avoids a myriad of concurrency problems and memory issues typically involved with multithreaded systems and completely avoids shared memory concerns that multiprocess applications suffer (typically).
It’s it the most efficient? No. Does it do multithreaded work nicely by default? Also no but it can, and likely will steal some ideas from go if warranted (as is the way of tc39). But, when you are already developing JavaScript fronted code, you can reuse parts of it in the backend, and more importantly: you don’t need additional knowledge of another language to maintain that system. This is good for productivity.
It’s good enough to do the job no matter your opinions on the subject.
2
u/_default_username Oct 03 '22
have you used a monorepo with typescript? That's where I saw the value. we had generated types in our api and we shared those types with the frontend, so when we made api requests the payload would be typed.
3
u/whatarewii Oct 02 '22
We use TS for both, all out React is built using TS and same with our backend. No vanilla JS in sight, this is my first time working with TS but coming from mainly strongly typed languages I love it
6
7
11
u/elcapitanoooo Oct 02 '22
TS is probably the biggest improvement for webdev since jQuery. But as its a language of its own, it will be around for much longer than jQuery did. I never felt confident with untyped languges, so for me TS is a musthave for any project. Good times!
5
u/ECrispy Oct 02 '22
Anders is one of the unsung geniuses. Not many know how influential the is and no one knows his name in the dev community unlike many others.
-2
u/mthompson100 Oct 02 '22
I’m sure I’ll get downvoted for this, but TypeScript is useless on the front end. There is some value to it when doing server side code. On the front end, I prefer React PropTypes. I have over 10 years of development experience. I started using TS about a year ago when I switched jobs because that is what the company forces everyone to use. I’m slower when writing with TS, I don’t see a difference in the number of bugs, and TS errors are often very cryptic.
10
Oct 02 '22 edited Oct 02 '22
There's often a lot more going on in the frontend than props though. TS lets you type your hooks, state management, and utility functions. Third party libraries become easier to use too because their interfaces are typed.
edit: wow you downvoted each reply to your comment immediately. petty much?
6
u/mthompson100 Oct 02 '22
The downsides that I see with TS:
TS requires a lot more code. It's takes more time to write, is verbose, and difficult to read.
TS error messages are difficult to understand.
It doesn't provide enough value to warrant the time spent writing it vs JS. With JS, use prop-types, basic unit tests, and Cypress to catch nearly all problems before they get to production.
The duck typing implementation sucks.
TS warns about null values in arrays even when null values have already been filtered out.
7
u/Educational-Lemon640 Oct 02 '22
I've seen this list before in response to TypeScript threads, and I think it deserves a response.
First I'd note that these comments are probably partially a result of different development environments and history. I'm an experienced software developer in a very large, very intense SaaS codebase. Our "client-side" code is a special type of crazy, so much so that we say that we have two backends: a "real" backend which actually runs on our servers and is written in Scala and a "client" backend which technically runs on the users' machine, but is pure model code. Our "client" frontend code is written in Angular, which also requires TypeScript. Our use of TypeScript fits into this use case very, very well.
That said, here are my responses:
"TS requires a lot more code. It takes more time to write, is verbose, and difficult to read." What you call "more code", I call "documentation." It's extremely useful for both maintenance programmers and tooling. Since maintenance programming is most actual programming, it's hard to exaggerate how important this is. This is also why "takes more time to write" is just not that important; the first version of the code is much less important than later use, and those later uses are far faster if the maintainer knows what it's supposed to be doing. As for "difficult to read", AFAICT that's mostly a matter of experience, but it's experience well worth getting. I find it makes code easier to read, because again, its expected behavior is much better documented.
"TS errors are difficult to understand". It's true that communicating errors is hard and the programming community in general has a hard time with it. That said, it would be nice to get examples of what you actually mean; this is not my experience. All errors are bad, but again, most can be learned with experience and I don't see TypeScript's as unusual. I definitely like how I get them before the bugs are triggered at actual runtime. In fact, with proper tooling, I can get almost instant feedback when I’ve made a mistake during the actual coding. It’s hard to estimate the number of bugs that don’t get into production because they never even got saved to the source code, but in my experience they are substantial.
"It doesn't provide enough value to warrant the time spent writing it vs JS. With JS, use prop-types, basic unit tests, and Cypress to catch nearly all problems before they get to production." Clearly this is partly a judgment call, but I will note that with TypeScript, there are entire classes of unit tests that you just don’t need because the tooling won’t let you create code that breaks the documented interface. Unit tests that you don’t need are a great boon to maintenance, because unit tests are code, and code requires maintenance. As for prop-types, I think the relationship between prop-types and TypeScript are best described here. (I hadn’t heard of prop-types before this comment; it’s a run-time type validation framework, which is of course very valuable, but very different from TypeScript in terms of actual use.)
“The duck typing implementation sucks.” I don’t think it actually sucks, nor do lots of other developers? This isn’t a very concrete criticism, so it’s hard to say anything about it. Certainly I had some early trouble getting my head about it, but now that I understand it, it’s extremely useful.
“TS warns about null values in arrays even when null values have already been filtered out.” This is a valid criticism, but it’s a bit of a nitpick. There is a solution; the solution is to annotate the filter function using an explicit type predicate, i.e. instead of the filter functions being (x: type |undefined): boolean => <code>, it should look like (x: type | undefined): x is type => <code>. TypeScript is still hindered by being tied to JavaScript, but you can generally make it work.
1
u/kongker81 Oct 02 '22
Part of being a good developer is being open minded to new ways, even if they may go against your initial beliefs. I had my doubts around Typescript before I realized the necessity of it for larger projects. And now I do not code in vanilla Javascript, even for frontend.
6
u/onaiper Oct 02 '22
But do you never benefit from changing some interface and the compiler telling you all the places in your code that are affected?
2
u/kongker81 Oct 02 '22 edited Oct 02 '22
Not everyone uses React though. And React isn't the bible to Javascript development. Nothing against the framework, but I feel like React is just way too overhyped. There are other good micro frameworks out there such as express.JS for javascript development. Something like express.JS is lightweight, and great for API development if you require a rest implementation.
But Typescript is great for defining your own Interfaces and Types, and the beauty of this is intellisense. Because Javascript is dynamic, you have to actually remember every single method or property you attach to an object and that gets unrealistic in big projects. For example, you can do this in typescript:
type Size = "small" | "medium" | "large" type Sound = "meow" | "neigh" | "ruff" type Type = "cat" | "horse" | "dog" interface Animal { sound: Sound; type: Type; weight: number; size: Size; }So now when you create a object, you have all of the attributes defined and bound. Typescript will also make sure you don't add a new attribute to the object, such as "myRandomValue". You now create your value like this:
let animal: Animal = { sound: "neigh", type: "horse", weight: 1000, size: "large" }And intellisense has you covered. No need to remember your attributes! For example, when you are typing in your attribute for size, intellisense will ask you what size? small, medium or large. Isn't that a beautiful thing? And if you add anything else such as:
animal.myRandomValue = 4You will get an error, which is what you want. Without typescript, you can add anything to object animal since it is not bound by a contract. The dangers of this is, if you decide you don't need myRandomValue anymore, you have to find all references and remove it yourself. But with Typescript, the errors will show you exactly where the instances are (if you decide to remove it from the interface) and won't even compile until you do remove the references.
Also, Frontend Typescript with webpack will compile to Javascript, so as long as you understand the setup, you can use Typescript for both frontend and backend debugging purposes in VS Code, using a custom debug config in launch.json.
1
4
u/RaphaelDDL Oct 02 '22
100% this.
The amount of bloat to be written to get some type checking and “ide autocomplete” isn’t worth it for front
On node does have merit as a typed contract means a trusted response, and also when dealing with DBs
2
u/hekkonaay Oct 02 '22
Some type checking and IDE auto-complete? People who say TS isn't useful to them have never worked on a large enough codebase.
1
1
u/RaphaelDDL Oct 02 '22 edited Oct 02 '22
Prop typing on vue is good enough, don’t need ts
It’s funny when people say “large enough codebase” since even a small codebase becomes LARGE when you shove all that bloatscript interfaces all around
I LOVE when every argument for TS always ends on “you haven’t worked on a large codebase” and/or “big companies use” like any of those has any merit..
They also used Java monoliths so what
Fyi I worked on fortune 500 for 7 years
3
u/Bushwazi Oct 02 '22
Downvoted and your check from Microsoft will now bounce!
I’m joking. Before the true believers start attacking me.
0
91
u/Educational-Lemon640 Oct 01 '22
TypeScript has been a game-changer for my company. I can't imagine what it would be like to still be working with untyped code and the corresponding lack of tooling.