Throughout the previous lessons, we've had data types in the back of our minds. There's been a distinction between numbers, strings, arrays, and other data types, but we don't always know the data type of something in our program.
For example, this function has no idea what the type of its arguments are:
It expects that
num2 are both numbers, but nothing's stopping us from doing something like this:
And getting strange results because of it. Even worse, we might not know what type a function returns. Maybe we use a library which doesn't exactly tell us what one of its functions returns. We could run it and look at its output, but that's a bit tedious and the mystery function could return different types. If we think it always returns one thing, but it actually returns another, it can lead to bugs that can only be solved by examining the logic of your program line-by-line. For example:
In this example, the
getNthPrime is returning a string, when we think it's a number. Because of this assumption, we add one to it as if it were a number, when in reality this sticks a
1 on to the end of our prime. Not only will this completely break our program, the problem could lie in someone else's code, which is a nightmare to solve. Luckily, we don't have to.
All we're doing here is saying that
num1 is a
number (as is
addTwoNumbers will return a
number. Making this little change makes our code a little smarter, and it will give an error if we try to put a string (or anything else that isn't a number) into
addTwoNumbers. Now, let's see our other functions:
And instead of creating a bug that wastes hours of our time, we just get an error saying that
addTwoNumbers requires a
getNthPrime return a
string. Also, keep in mind that our variable
primePlusOne can have a type (we'd write
const primePlusOne: number = ...), but it isn't needed because
addTwoNumbers has a type, and that gets carried over to
To install TypeScript, we need to run
pnpm add --save-dev typescript @types/node in our console. The
--save-dev means that this is a "dev dependency", something which we only need when developing our program, but anyone running it won't need it.
@types/node is a library that gives types to the built-in functions in order to make things a bit easier for us. After that, typescript is installed, but we have a bit more configuration to do.
The last thing we need to do is create a
tsconfig.json. A basic one that should suit your needs is included in this project.
tsc command, which will output code into the
dist directory. Next, we can run
node dist/index.js to test our code. In order to make this easier, we can create a "script" in our
package.json file. Take a look at
package.json, then try
pnpm run dev for a quicker run command.
Now, try to add types the
index.ts file (in the
src) directory. Good luck!