Can anyone recommend a good introduction/book to TypeScript for people who already know some programming (e.g. Java, Python) but have no experience with JavaScript?
The only texts I’ve found so far assume prior JS knowledge (like The TypeScript Handbook https://www.typescriptlang.org/docs/handbook/intro.html) or seem rather unstructured to me (e.g. dropping a lot of different ways and shortcuts to do things without explaining concepts or making no clear distinction between basics/best practices and advanced use cases/edge cases).
Ideally it would explain core concepts (like functions, types, classes, …) first, with their most common use cases. Later chapter would do deep dives into different topics.
Edit: when I’m talking about TypeScript I’m talking about the whole language, not just the “modifications” it makes to JavaScript.
I know it’s not what you want to hear, but you should really learn JS first and then go through the handbook you linked to learn TS. As you’ve noticed, the majority of the resources for TypeScript will assume that you’re already proficient with JS and you’ll have an easier time with the ecosystem if you take that approach.
seem rather unstructured to me (e.g. dropping a lot of different ways and shortcuts to do things without explaining concepts
That’s because there’s no way to explain TS concepts without the JS context, so any attempt to skirt around JS it’s going to feel like there’s a big part missing. You should think of TS like a way to write JS differently, it’s mostly a replacement for JSDoc/linting rolled into a nice package. It’s not a fully standalone different language.
The closest comparison would be type hinting in Python – it’s useful but there’s no way you can use Python knowing just type hinting.
Like I said in my other replies: I am not attempting to skirt around JS. If TS is a superset of JS, it would obviously make no sense to try to avoid JS. What I’m looking for is a book/tutorial/… that is structured in a way that it teaches the most important parts the way they are used in TS (including JS) first.
Example: when functions are taught it would explain the basics of JS functions along with the parameter type annotations of TS, because that’s the way functions are used in TS.
There just isn’t much use for an approach like this, unfortunately. TypeScript doesn’t stand alone enough for it. If you want to know how functions work, you need to learn how JavaScript functions work, because TypeScript doesn’t change that. It adds some error checking on top of what’s already there, but that’s it.
An integrated approach would just be a JavaScript book with all the code samples edited slightly to include type annotations, a heavily revised chapter on types (which would be the only place where all those type annotations make any difference at all, in the rest of the book they’d just be there, unremarked upon), and a new chapter on interoperating with vanilla JavaScript. Seeing as the TypeScript documentation is already focused on those exact topics (adding type annotations to existing code, describing how types work, and how to work with other people’s JavaScript libraries that you want to use too), you can get almost exactly the same results by taking a JavaScript book and stapling the TypeScript documentation to the end of it, and it’d have the advantage of keeping the two separate so that you can easily tell what things belong to which side.
I would probably recommend “Just JavaScript” as a way to develop mental models of JS, then the TypeScript handbook to learn the core principles behind typings.
Not what you asked for, sorry, but probably where I’d start.
I’ve come from many years of Java experience to the world of Node/JS.
You can learn JS without TS, but not TS without JS.
TS is just a tightened up JS really.
Yes, and I would like to learn the tightened up version rightaway ;-) When I want to learn e.g. about functions, I want to have a chapter “Fundamentals of functions” or something (and probably another chapter for advanced stuff about functions, edge cases etc.). Right now it seems like I would have to first read about functions in a JS book without knowing how they will be used best in TS). And then I would take another book and read about the modifications that TS makes to them.
I get that in order to “fully understand” TS I need to “fully understand” JS. But in the beginning I would like something that explains the core concepts of TS (which of course may and often will include JS concepts).
@homoludens I don‘t think is a good idea. Every TypeScript developer ought know deeply JavaScript. For example, if you work with Django you ought programming with Python.
Yes, that I need to learn some amount of Javascript is a given. But I would rather not learn some JS aspects that I won’t need anyway, because TS prevents me from using them or has other best practices.
Typescript doesn’t really remove anything you learn in JavaScript. Like at all. It’s not really a library as such. It adds ways to enhance your JavaScript, with typing, structure, and tooling
Learn JavaScript as much as possible. Every bit you learn will benefit you with typescript
I know TS doesn’t remove that much from JS, but I expect the typing, structuring etc. to prevent some behavior that can occur in JS - otherwise what would be the point of e.g. the typing system? So that are the parts I don’t need to learn (at least not at first).
I think an important point for me is that I’d rather learn “from a TS perspective”, that is starting with best practices and common use cases as they appear in a TS environment. Right now it sounds to me that the usual way would be to read some JS book, where I learn e.g. about functions or objects. And then I would read another book with all the modifications that TS makes to e.g. functions or objects.
Its important to understand that:
- JavaScript is typescript
- Typescript is JavaScript with types
When you are writing typescript, you are writing JavaScript but have additional syntax to help support type safety and structure. If you are creating a function that does x, it should very much be the same in JS and TS, just in TS it has extra syntax
TS doesn’t modify the way JS works, its one of the stated needs for the tooling.
In TS, for example, I can denote an object as
const x: Record = {}
In JS it would be
const x = {}
It’s still nothing but an object. TS doesn’t change the functionality, it just adds typing and checks that you are using that object properly as static build step.
otherwise what would be the point of e.g. the typing system?
What’s the point of type hinting in Python?
@homoludens Can you give me some example?
Please see my other replies.
As many people said here, I’d recommend getting more familiar with JS and then jumping to the documentation which has great resources like this one.