TypeScript
TypeScript is Microsoft's attempt to impose some sanity on JavaScript's wild type behavior. TypeScript is a superset of JavaScript that can't run directly in a web browser and in most server runtime environments. It needs to be compiled first, and making the compiler happy enough to complete your build can be challenging.
If you're brand new to programming, it probably doesn't make sense to start with TypeScript unless you are a real pain pig. If you're already an experienced developer from another area (games, that sort of thing) who needs to do some web dev, TypeScript will make a ton of sense.
The extra features TypeScript gives you are very nice:
- Syntax for types in your code.
- Type checking and type inference in your code editor.
- Eliminate a whole class of errors associated with JavaScript's dynamic typing.
If you write JavaScript with Visual Studio Code, you're actually already using TypeScript, sort of. Under the hood, VS Code uses TypeScript to power your JavaScript editing experience, even if you're not actually writing TypeScript code.
TypeScript learning resources
The TypeScript Handbook is a great and has different content depending on the language you're coming from. Microsoft has put a great deal of effort in their TypeScript content over the years, and it shows in the handbook.
If you prefer a video experience, freeCodeCamp.org has a great 5 hour YouTube course called Learn TypeScript - Full Tutorial.
Personally, I found the best way to learn the language was to just start scaffolding my React apps in TypeScript and then searching when I hit an error I didn't understand.
TypeScript errors usually include a code like TS1234
that you can search to find pretty good results on Stack Overflow. AI models have had enough experience stealing Stack Overflow answers that they are pretty good at helping you resolve things too.
What sucks about TypeScript?
Hoo boy, how much time do you got?
TypeScript's compiler tsc
exposes dozens and dozens of different options and every single project is free to tune these how they like. This makes things really customizable, but good luck finding a solution to your particular problem if you've changed tsconfig.json
. Hell, good luck finding a solution if you haven't. Apparently this is a "skill issue" and not a horrific design flaw. I guess I'll go fuck myself!
Microsoft being Microsoft, they've also added way too much to the language over the years to satisfy different genres of OOP freaks and enterprise software enthusiasts. JavaScript already has the biggest surface area around, and TypeScript doesn't do anything to narrow it.
Types are valuable, and there is actually a TC39 ECMAScript Proposal called Types as Comments that might someday add types to JavaScript that would be ignored like comments at runtime. Hopefully we will be able to have our cake and eat it too.
But writing TypeScript as it is today can be a huge pain in the ass. If you're writing library code (ex: code that will be installed into other applications), chances are your consumers will really want your type information and will raise hell if it's missing. If you're writing application code and making the TypeScript compiler happy is wasting cycles, maybe try the middle ground of JSDoc comments. These can give you type hints without the compiler stopping you from shipping working code that (horror of horrors!) could include having to deal with a variable of type any
.