Join the DZone community and get the full member experience.Join For Free
You may also like: New Features in TypeScript 3.7 and How to Use Them.
It’s much simpler to abandon JS when starting a new project, where you don’t have to worry about things like retro-compatibility, or an app’s maintenance in production. In that case, you can try many alternatives, and choose the one you like the best.
Now, I will present a simple way to add TypeScript to a project, without the need to modify our configuration of webpack, gulp, or whatever our build system is.
Assuming you use npm as a package manager in your project, first thing we need to do is to add TypeScript as a dependency (if not, you can install it globally):
Note: depending on what your project is, you might also want to install “@types” for other libraries you have dependencies on. For example, if you have a react-redux project, you might need to install the following:
After that, we need to add a tsconfig.json file at the root directory of the project. That file contains compiler options needed to convert TS to JS. In order to have the least issues, use the following configuration to make JS code compatible with TS:
Note: You might need to change some bits based on your project. More on that here.
Now, add the following script in your package.json:
And run it. It will run a watcher that transpiles all .ts (or .tsx) files into regular .js files. Also, it will generate these files at the same path that of original; therefore, all imports and all build processes you might have will work as before, since the .ts files are completely ignored, and the result of the transpilation is used instead. The generated file structure has the following structure:
Now, all we need to do is to create our first ‘.ts’ file by changing the extension of an existing one that we want to migrate to TypeScript. Alternatively, we can also create a blank file to start working in our application.
This does not bring much change. We still can put normal JS code and get no help from what TypeScript has to offer. In order for TS to force us to actually type our code, we need to change the tsconfig.json file. In particular, we will focus on two compiler options that will force our code to be actually typed:
Let’s imagine we have a simple mortgage simulator that tells the user if, given his financial situation, a mortgage is viable or not. For that, we will have a function that will retrieve somehow the savings user has:
And a function to decide if a mortgage is feasible:
But, to make it actually work, we would need to check if the input exists. And also if it’s a number or not. The same applies for the return value from
getSavings, since we don’t know the return type of that function either. Therefore, our code could end up looking something like this:
This looks quite terrible.
But, if we activate the
noImplicitAny compiler option, it would no longer be necessary to check if the input value and the return from
getSavings are numbers, so the function could look something like this:
This is an improvement, since the compiler can help us to avoid some mistakes and typos, not allowing us to call the function with a string for example:
Unfortunately, null and undefined are still in the domain of every type, therefore it would be possible to do this:
To fix that, we need to activate the other option in the tsconfig.json file we mentioned,
Now, doing the call to the function with null, would end up getting caught by the compiler:
That means that we don't need to check for null types, which simplifies the logic to something like:
This is just a small glance at what TypeScript can give you if you migrate your project from plain JS to it.
Published at DZone with permission of Roman Predein . See the original article here.
Opinions expressed by DZone contributors are their own.