r/javascript • TypeScript • Oct 01 '22

Ten Years of TypeScript

https://devblogs.microsoft.com/typescript/ten-years-of-typescript/
255 Upvotes

49 comments sorted by

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.

12

u/AKMarshall Oct 02 '22

TypeScript has been a game-changer for my company.

It is a game changer for Microsoft too, since now web developers are on the Microsoft stack (Typescript, VSCode, Github, NPM, etc).

21

u/satinbro Oct 01 '22

unTypeScript

34

u/evonhell Oct 01 '22

Yeah, building enterprise size projects without TS just feels irresponsible at this point. It's such a huge help for us in so many ways. I use TS in my smaller projects now too because it's just easier to think about things being typed now. Not coming from a typed language it took a few weeks for the whole thing to finally click for me, but once it did... There's no going back

5

u/onaiper Oct 02 '22

Honestly, it's hard to go to some of the typed languages because of how much worse/less flexible their type system is. In terms of pure ease of development it's really hard to beat a highly dynamic language with types.

2

u/serg06 Oct 07 '22

Yes! It blows my mind that the basic features I've gotten used to, like union types, are completely missing in modern languages like Kotlin.

2

u/onaiper Oct 07 '22

Yeah, and higher-order types like Partial, Pick, Omit, etc...

The amount of transformations you can apply to a type in TS is amazing.

I actually don't know of another language where you can so easily declare that some function relies only on certain fields of a record. Or where a function can work on multiple overlapping types. I suppose the overlapping types thing can theoretically bite you in the ass, but I think the benefits outweigh that.

3

u/abrandis Oct 02 '22

But why can't typescript be integrated natively Into browsers and Node ?

Before folks start in about Typescript is for programmers not interpreters rant.. that's true , but having TSC integrated into the interpreter makes sense , it could just automatically run (TSC on changes ) and cache that code . It would cut one more tool out of the already bloated toolchain , finally maybe once day browsers will ditch native JavaScript and right a true native Typescript engine...

4

u/Booty_Bumping Oct 02 '22 edited Oct 02 '22

https://github.com/tc39/proposal-type-annotations

The above is a proposal to bring type annotation syntax into the Javascript language, but without actually specifying what the types mean — the presence of type annotations would be a no-op, like a comment. It would allow typescript/flow code to run in the browser and node.js, but no checking will actually be done unless you use typescript or flow. So it's very similar to what Python has done.

2

u/Educational-Lemon640 Oct 02 '22

It was mostly because the vast majority of people who used it needed to support multiple browsers, and that required backwards compatibility. Old browsers, especially IE, were especially "popular" for this need. It's not great, but the world is what it is.

There is also the fact that commercial products also need an obfuscation step after the transpilation step, and that was explicitly not part of TypeScript's mandate, for good reason.

There's no reason why what you suggest can't actually happen. It just hasn't been a priority, for non-technical reasons.

2

u/IceSentry Oct 02 '22

Because the types aren't enforced at runtime so that would mean you are shipping a lot more code to your users that serves litterally no purpose. Sure, the dev experience might be a bit nicer if you don't need a build step, but shipping it to users just makes no sense. It's not that browsers can't, it's that browsers have no reason to do that.

As for node support, just use deno if you want native typescript support.

-23

u/ILikeChangingMyMind Oct 01 '22

I'll get probably downvoted for giving <3 to JS in a TS thread but ... it's pretty awesome.

For one thing, we don't have to write types :) But, unlike the old days, we still get most of the benefits of our libraries being typed, liked autosuggestion and automatic imports ... if we use VS Code and checkjs: true.

23

u/acemarke Oct 01 '22

Note that VS Code itself uses the exact same TS implementation to be able to do that - checkjs is explicitly a TS compiler option, and TS is used for all of VSC's JS processing in general.

2

u/ILikeChangingMyMind Oct 02 '22

Yes: 80% of the benefits of TS, for (less than) 20% of the effort of actually using it!

32

u/[deleted] Oct 02 '22

Yeah you're just using Typescript and don't even know it lol.

2

u/ILikeChangingMyMind Oct 02 '22

No, I very much know it, and appreciate getting most of the benefits ... without any of the work.

15

u/xroalx Oct 02 '22

For one thing, we don't have to write types :)

See, that's actually the downside. It's not that you don't have to, you can't, so when you define your own functions? It's either the new dev having no idea what they accept and return, or using clunky JSDoc.

With TypeScript, not much changes, except that you can, but don't have to, write types more naturally. That way, the new dev will know what the functions of your codebase accept and return, and that's much easier to work with.

TypeScript might not be that appealing to you when you're the sole person working on a hobby project, but when you get into an organization that has their business built on JS and no JSDoc whatsoever, that huge codebase will be a very brittle and painful thing to navigate.

Basically, you'll never know what's happening without actually reading all the code, and I shouldn't read the code of every damn function just to make sure it doesn't return something else in an edge case. I'll be able to tell that just from the TS declaration of the function.

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.

https://web.archive.org/web/20121003001910/https://blogs.msdn.com/b/somasegar/archive/2012/10/01/typescript-javascript-development-at-application-scale.aspx

4

u/[deleted] Oct 02 '22

Are most of the die-hard typescript supporters working in nodejs or front end frameworks?

8

u/[deleted] 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

u/[deleted] 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

u/[deleted] 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

u/[deleted] Oct 02 '22

Hey, you’re the author of this post! Very nice, thank you

7

u/gaytechdadwithson Oct 02 '22

Well it doesn’t look at day past 5

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

u/[deleted] 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:

  1. TS requires a lot more code. It's takes more time to write, is verbose, and difficult to read.

  2. TS error messages are difficult to understand.

  3. 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.

  4. The duck typing implementation sucks.

  5. 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:

  1. "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.

  2. "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.

  3. "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.)

  4. “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.

  5. “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 = 4

You 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

u/itsjustausername Oct 03 '22

You have added a nice spell check there.

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

u/30thnight Oct 02 '22

"You can't miss what you never had"

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

u/Puzzleheaded_Toe117 Oct 02 '22

Fuck typescript. Fuck ms