Thereâs TypeScript and Then Thereâs TypeScript
Are you a TypeScript user?
Your initial reaction may be a resounding âYes!â
Or perhaps be a booming âNo!â
The answer seems simple, but (as with most things) it might be more nuanced than you think.
Hereâs Anders Hejlsberg, TypeScript Co-Creator & Lead Architech, from the TypeScript documentary:
If you use VSCode and you write JavaScript, then you are a TypeScript user because the language service that powers anything that happens in there is the TypeScript compiler. Thereâs TypeScript the syntax and then thereâs TypeScript the tool.
Itâs easy to think of âTypeScriptâ as âthe syntaxâ. Either you use TypeScript and write .ts
files, or you shun TypeScript and exclusively write .js
files.
But, to Andersâ point, if you write .js
files in VSCode youâre âusing TypeScriptâ because thereâs more to TypeScript than just syntax: thereâs TypeScript the syntax and then thereâs TypeScript the tool.
TypeScript the syntax is a superset of JavaScript. It has annotations, declarations, and more, all layered on top of JavaScript.
But you need a tool, e.g. a compiler, to erase those additions so that the code you write becomes standard JavaScript that can run in the browser (or some standards-based ECMAScript runtime).
Thereâs also TypeScript the tool. It is perfectly happy to work with regular oleâ JavaScript. It can analyze JavaScript and infer types. It can analyze JavaScript with comments in the form of JSDoc annotations to get types. It can get types from declaration .d.ts
files. Or it can do all three at once from different files in the same repository â all to make that information useful to the VSCode user.
Thatâs sort-of the beauty of TypeScript: you can use the flavor that works for you.
If youâre keen on writing code as it will be run, you can still âuse TypeScriptâ without a compilation step (write JS in VSCode, for example).
Hereâs Ryan Cavanaugh, TypeScript Engineering Lead, from the same documentary:
When I hear about big projects like Svelte and Webpack only using JSDoc, some people look at that and say, âOh theyâre moving away from TypeScript.â But I say, âNo theyâre embracing TypeScript. Theyâre embracing this other flavor of TypeScript that weâve made.â
What stood out to me from the documentary was how members of the TypeScript team note that, with things like the type annotations proposal, the days of writing .ts
files might actually be numbered.
Eventually, âusing TypeScriptâ could merely become running a CLI over your .js
files and making sure everything checks out. You wouldnât have to do that, you could skip that step entirely and run your code without any analysis, but you could also run it for safetyâs sake â a kind of âcompiler via commentsâ.
That's what the proposal could bring, which intrigues me because it shows how the TypeScript team is aiming to make the .ts
to .js
aspect of the tool follow the steps of Sass or jQuery: a tool making itself obsolete by disappearing into the web platform.