If you already have a subscription, you can sign in.
Enjoy free content straight from your inbox 💌
00:00
Coming from other type programming languages, you might be familiar with the concept of typecasting. Here we have a variable of an undeclared type that we later initialize to a string. And then if you want to use this variable as a number, you might be tempted to use a type assertion of TypeScript to convert it to a number. However, a type assertion is not the same as type casting. This is because the value currently held by the variable will still be a string. And if you try to compare it to a number at one time,
00:32
you will find that it is not a number, 'cause its value is actually the string. 1, 3, 3 7. Now, TypeScript doesn't support any special syntax for type casting, however, it understands how type casting works in JavaScript. So you can use your JavaScript tricks to do any type casting that you need. For example, a common way to convert a string to a number is using the plus operator before the string. Now TypeScript understands the impact of using the plus operator. So now if you hover over the number variable, you can see that TypeScript has inferred
01:04
that it is going to be a number. Now this type of type manipulation within JavaScript documentation is called type coercion. And with that in place, the variable number will be a number at run time.