Check out a free preview of the full TypeScript Fundamentals course:
The "Implicit Typing" Lesson is part of the full, TypeScript Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Mike discusses how TypeScript can guess types through the assigning of a value to a variable.

Get Unlimited Access Now

Transcript from the "Implicit Typing" Lesson

[00:00:00]
>> Mike North: So one of the great strengths of the TypeScript compiler is that is uses implicit typing. Unlike other strongly typed programming languages, we don't necessarily need to define the type upfront in our code. In this situation here, we've got teacher's age equals 34, and we know 34 is a number.

[00:00:23] So we have enough information to say teacher age, that variable's type is number. If we then, try to assign teacher age to another value that's not a number, we're gonna get an error. And, what we're looking at here, is the idea of type equivalents, right? It basically, is trying to figure out if this string thing can be converted into a number, somehow.

[00:00:48] And without any additional information, like in this case, we would need to explicitly parse int or something, right? To make that conversion work. So, this is one of the first things you'll see about TypeScript, that after assigning, after a variable gets a type, you're not allowed to change that type.

[00:01:07] And again, we'll type, a the result of a the assignment.
>> Mike North: JavaScript does let you flip-flop between types, right? You could have run this code without incident in JavaScript, and it would have been fine. But that is the kind of thing that can lead to de-optimization. And if you have the slides and click that link.

[00:01:25] There's a great blog post about it comes up, called Hidden Classes In Chrome that describes some of the stuff that I was just talking about. The hot code versus hot and cold paths [LAUGH]