DEV Community

Ben Lovy
Ben Lovy

Posted on

TypeScript before JavaScript?

Shiny objects

Am I shooting myself in the foot by learning TypeScript without properly learning JavaScript first?

When I started taking programming seriously I thought I wasn't interested in web development so I focused on getting productive with C and Rust and dabbling in Haskell - but rather quickly discovered that things are different now then they were in 2004 when I first tackled a website. The web is a whole new platform and it's ridiculous to avoid it outright. You cannot beat the portability it offers.

This kicked off a largely fruitless chain of hipster tech. I built little prototypes in ClojureScript's Reagent, Re-Frame, and Om. I built a few Elm toys. I tried PureScript's Halogen and Pux. I tried bucklescript-tea and ReasonReact. I built an app in yew, an experimental framework for Rust targeting WebAssembly, which, while cool tech, was definitely a weird use of time figuring out how to hook everything up for someone who'd never done it in JavaScript before - learning yew taught me about React before I'd ever looked at any React code. I even spent a ridiculous about of time messing around with GHCJS to write my frontend code in Haskell - my poor old 2011 ThinkPad deserves a medal for that week.

The writing was on the wall, though. If I'm going to take this industry seriously and eventually switch careers, I cannot avoid learning JavaScript. Being able to read snippets is all well and good but I've gotta learn how it really works, and I'm not even honestly sure why I've been avoiding it in the first place.

However, I'm still having trouble giving up my types. My favorite of the above menagerie was Re-Frame except for the lack of types. Clojure is a joy to write but a pain to debug - I still spent the vast majority of my development time tracking down stupid errors at runtime that I'm used to having caught for me, or sifting through opaque Java stacktraces with very little relevant info. Part of me thinks this is a lack of experience with dynamic languages - almost everything I've used has been typed and compiled, with the sole exception of Clojure. Which suggests I should invest more time in learning how not to do that.

Enter TypeScript - at face value it's everything I want. It uses ES6 constructs, so I can learn about those, and has a pretty advanced type system to boot. It's object-oriented in a way that's more similar to the big industry-standard OOP systems - something I don't have a lot of exposure to or practice with. All of the JavaScript bundlers and testing libraries and what have you still apply. The way I see it, learning TypeScript well is kinda like learning both for the price of one. All I'd need to do to adapt to a JS codebase is rip my types out.

Is this disingenuous? I'd love to hear what people who already know and use these technologies think. Comfort-zone-wise I'm completely content to continue using TypeScript on my personal projects, and it seems more and more that it's being adopted by larger companies and frameworks. If that's holding me back, though, I should start learning JS proper now!

Top comments (26)

Collapse
 
bhaibel profile image
Betsy Haibel

Hmm. We often think of TypeScript as a superset of JavaScript -- as JavaScript with types. But it's just as valid to think of it as a subset of JavaScript. It's JavaScript without the dynamism that skipping a type-checker can give you. There are all kinds of dialects of JavaScript, and only some of them are compatible with the style that TypeScript enforces. So, you might not learn how to think about JavaScript written in different styles!

I'd also be concerned about not really learning what the border between JavaScript and TypeScript is. I work with a lot of early-career Rails developers. Folks who learned Ruby first and then Rails tend to be more successful. Folks who learned the two of them at the same time often really struggle with figuring out what's Ruby and what's Rails. Not knowing what Ruby is "on its own" can really distort their sense of what's possible in Ruby.

I'm not saying that you should put off learning TypeScript! You sound pretty enthusiastic about it. But learning only TypeScript might mean stumbling into some uncanny valleys when you start working with Other People's JavaScript.

Collapse
 
deciduously profile image
Ben Lovy

First off, thanks for the back-and-forth :) I really appreciate it!

That's a great point - I think I've already started running into that fuzzy boundary when I've tried using frameworks. Most of my TypeScript experiments have been pretty vanilla, just learning how the canvas works and things like that, but a few things I tried to do with a TS/React project proved trickier than expected.

It seems like, once again, the fabled missing ingredient is Other People! Funny how that works. Another goal of mine for the near future is a first PR, maybe I should focus on some plain JS codebases to explore.

Collapse
 
bhaibel profile image
Betsy Haibel

Give yourself some credit -- the canvas can get pretty vicious once animation frames are involved!

Thread Thread
 
deciduously profile image
Ben Lovy

Oy - I more meant vanilla as in free of external dependencies but you are not wrong!

Collapse
 
kayis profile image
K

I always try to learn all I need to know to get a job done, then I try to learn the details when I need them.

Do you have a job that needs TypeScript? Learn it.

If you don't have a job that needs TypeScript, then dont learn it.

If nobody tells you if you need it or not, try to figure out the expected complexity of the project and if you comfortable with it, use JS, if you aren't comfortable with the expected complexity, then use TS.

Collapse
 
deciduously profile image
Ben Lovy

That's the thing - I can't tell what does or doesn't need it, and all of my programming experience has been statically and strongly typed. So it sort of feels like every project needs it to me, but that's obviously not the case.

Collapse
 
kayis profile image
K

If you wanna easy going with coding, you will probably have more luck with TypeScript.

Programming with JavaScript is basically programming with untagged unions only, which feels like a nightmare for many devs.

Since you'll need to bundle up your JavaScript anyway, you can go with the TypeScript tooling, as it won't add much more overhead as the JavaScript tooling.

Thread Thread
 
deciduously profile image
Ben Lovy

Easy going sounds good, but not at the expense of actually understanding my code. That does sound like a nightmare, but if that nightmare is the state of the art I'm just prejudiced.

Really appreciate your thoughts! Agreed about the tooling - seems like a six of one sorta deal

Collapse
 
cloverich profile image
chris

If you already understand type systems, then typescript is all upside. It'll help you learn the JavaScript and DOM apis quite a bit faster in my experience (7 years of JavaScript, 2 years typescript). I think the only people that should wait in typescript or javscript newbies who don't know type systems either

Collapse
 
deciduously profile image
Ben Lovy

Cool, thanks! I do feel I have a good grasp of type systems from Rust and Haskell, but I will say that TS's structural typing is pretty foreign to me. It's cool seeing different ways of solving that problem.

Collapse
 
michaeljota profile image
Michael De Abreu

Sometimes when we start learning Typescript we asume it will behave just like a regular typed language, but it won't, because at the end the browser will be consuming Javascript, so you definitely want to learn how Javascript works and then you will understand better what is actually Typescript doing for you.

Collapse
 
deciduously profile image
Ben Lovy

That makes a lot of sense! It's definitely the most unique type system I've used because of how gradual it is - I can definitely understand the benefit of understanding the underlying systems first before tacking something extra on top.

Collapse
 
lyfolos profile image
Muhammed H. Alkan • Edited

First, you should learn javascript for sure. There is flow by Facebook, that implements static type checker like Typescript; It's a preprocessor. Typescript has more success on types. I do not recommend it (before javascript). There is many many languages transpiles into JS, like Reasonml/OCaml (OCaml is one of my favorites), Elixir (I didn't have tested it but I'm sure it's good), Clojurescript (Has really good interop) etc.

Note that: I'm a JS hater.

Collapse
 
deciduously profile image
Ben Lovy

Thanks for the insight! I'm already familiar with ClojureScript, ReaonML, and OCaml, and have looked at Elixir -- why do you not recommend typescript if you say it's more successful than Flow?

Collapse
 
lyfolos profile image
Muhammed H. Alkan

(Speaking assuming you know functional programming) It's mostly based on Object-Oriented Programming, and many codebases are written in Javascript and there is a possibility to not understand the JS code because of OOP concepts. Flow helps you to check types without changing the JS code. So it's better to learn JS or any other language because of your similarities.

Thread Thread
 
deciduously profile image
Ben Lovy

Gotcha. I'm fairly conceptually comfortable with both paradigms, but you bring up a good point. The OOP I know how to use is not JavaScript's prototypical variety, and that's definitely a potential source of confusion.

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

Here is another question. What if JavaScript was invented after typescript as a subset. What would that world look like?

Collapse
 
deciduously profile image
Ben Lovy

Interesting. Would it still be co considered a subset? I believe (though I'm no math guy) that the set of possible JS programs is larger than the set of possible TS programs.

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

Lots to consider here, I don't think JavaScript would exist at all. Options typing makes typescript equal to JavaScript. Okay what about if JavaScript had a type system that could be enforced. Typescript would become redundant?

Thread Thread
 
deciduously profile image
Ben Lovy

I don't think JavaScript would exist at all

I think I agree. Typescript exists to be an enforceable type system for JavaScript, so if such a thing was already built in we wouldn't need something else

Thread Thread
 
adam_cyclones profile image
Adam Crockett 🌀

You underestimate the developer community 🤣 there is a reason that reinvented of wheels is a common theme. But you have to ask, is the wheel the most optimal shape? So many questions, so much to write about!

Collapse
 
wolfhoundjesse profile image
Jesse M. Holmes

Kindred spirits, we are!

TL;DR
I went backwards, and I turned out okay. Also, I did brush up a lot on JavaScript fundamentals during my interviewing process.

Collapse
 
deciduously profile image
Ben Lovy

In more ways than one, though my low brass days are long gone now.

Thanks for sharing your write-up! Thats good to know... I've really been wanting to learn more about Angular but there's only so many hours in a week. I think I only went for react first because I'd used the CLJS analogue - Angular seems a bit more up my alley!

Collapse
 
galdin profile image
Galdin Raphael

I don't know JavaScript, but love typescript :)

Collapse
 
martinhaeusler profile image
Martin Häusler • Edited

Don't worry - you can't go wrong with TypeScript. You'll learn a bit more syntax, but you also get better tooling (code completion, error checking etc.) out of the box. I would advise to ALWAYS code in TypeScript instead of JavaScript. If you absolutely can't and you have to use vanilla JS for some reason, all you need to do is to drop the type declarations and change the file ending.

You do NOT waste your time learning TypeScript.

Collapse
 
deciduously profile image
Ben Lovy

Cool! Thanks for the feedback - that was my assumption, at least! Betsy brought up a great point in that there's more valid styles of JS than there are of TS but I still want to learn best practices first