Fixing Errors in the `add` Function
Common sense tells us that the boolean
s in the add
function should be replaced with some sort of number
type.
If you are coming from another language, you might be tempted to try using int
or float
, but TypeScript only has the number
type:
function add(a: number, b: numbe
Transcript
00:00 So Common Sense tells us that ADD here should take two numbers. A should probably be a number and B should probably be a number. We can remove these type annotations if we want, but we get a different error here. And the errors down the bottom don't go away.
00:15 So you might think, why don't we just rename them both as number here? And this seems to work. You notice that we can't choose like integer here. We can't choose float, which might be present in other languages. TypeScript just has the number type. And now everything is working.
00:32 And if we try to call something without, or try to call ADD with something that wasn't a number, then we would get an error here because argument of type string is not assignable to type number. So this is just your first little glimpse into the power of TypeScript's type system. We have here a function, which we are now being enforced to take in two numbers.
00:52 And we know that the result here also will be a number too, because the result of adding two numbers A plus B up here is a number. So there we go. Our first glimpse into TypeScript's type system.