If you're thinking about learning Typescript but not sure about the values it brings to the table, or you're already learning it and need to understand better its purpose, here I try to give a brief overview about what Typescript is.
Consider how many operations we can run on a
//we can try to get message length: message.length //we may attempt to call message with an argument: message("Hello World")
But wait, what is
message exactly? What is the Type of the message variable? Is it a string? an array or a function? How do we even know
message takes an argument and the argument is a string?
TypeError which will suggest to us that we were wrong about the type of the variable.
So you can imagine if
message was declared as:
const message = “Typescript is fun”
the first line of code would work, but the second would throw a TypeError telling us
message is not a function.
message at runtime (while the program is running) and thus cannot tell you if there's a problem with your code before you run it.
Now if you're working with a small codebase you may think that's not a problem not having everything typed, but things get messy pretty quickly with little bugs here and there as your codebase starts to become larger. It would be great if we could solve that problem. That's where typescript comes in.
static type checking
ahead of time if there's something wrong with your code as it relates to how you attempt to use different types.
Coming back to our example earlier, when we tried to call message with an argument.
const message = “Typescript is fun” message("Hello") //(Error) This expression is not callable.
Typescript would show a red squiggly line under message and would tell us
This expression is not callable. Type 'String' has no call signatures.
So we can infer from this message that our variable is not a function but a string, attempting to call it will break our program.
message is a string, we'll learn more about explicit types in a later post and how typescript deals with them.