Converting Enterprise Applications to TypeScript

Clayton Terry JavaScript, Single-Page Application, Technology Snapshot, TypeScript Leave a Comment

We have all been there: some new technology comes out and we know it would improve our maintainability. But we can’t use it. We already picked a technology, it is already implemented, and we can’t change it now. We are stuck.

TypeScript does not work like that. TypeScript is JavaScript, and JavaScript is TypeScript. What this means is any existing files you have are compatible with any new file you add.

So why waste time changing if it’s all the same anyway? TypeScript has some great advantages that JavaScript does not. In this blog I will introduce some of those advantages, as well as some troubleshooting tips for if you run into issues when you choose to convert to TypeScript.

All About TypeScript

TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. It is open source and compiles to clean, simple JavaScript code which runs on any browser, in Node.js, or in any JavaScript engine that supports ECMAScript 3 (or newer).

Yes it is true that TypeScript is simply JavaScript with a different file extension. But having actual types allows for simpler development. It allows your IDE to build and use intellisense, making development faster.

TypeScript is built around object-oriented concepts, in contrast to JavaScript which is more functional. The language is also compiled, which is like having a second set of eyes on your code.

Each TypeScript file begins with a class, just like a typical Java or C# file. It then is compiled into an IFFE. Keep this in mind when you are working. Once you understand how TypeScript compiles a class into an IFFE, it is very straightforward to convert an IFFE into a class.

Another convenient feature is Enumerations. These are great because it is almost identical to their C# and Java cousins. Meaning you can copy them directly from their definitions in services with little-to-no tweaking.

     

TypeScript also features Interfaces. This helps with intellisense, but it is also used for dynamically-typed files.

These special .d.ts files allow TypeScript files to interact with non-TypeScript files. And this is where the conversion begins.

Conversion To TypeScript

The simplest way to begin is to use your smallest IFFEs.

Build them into a TypeScript class.

All TypeScript classes require a constructor() method. Constructors work exactly the same as they do in other languages.

Keep in mind that TypeScript allows for inheritance the same way C# and Java do. So you may be able to reduce the amount of code you have to convert by building base classes.

All JavaScript is typed as any in TypeScript unless otherwise specified in a .d.ts file.

Troubleshooting Tips

If you are having issues with the conversion, define the issue method or property in an interface and type it as ‘any’. This can eliminate most issues.

If you are still having issues, it might be simpler to only use TypeScript going forward. It should still integrate seamlessly into your application, and once you are more familiar with the framework you can begin conversions of older code.

Final Thoughts

Enterprise applications usually require developers to keep to the one technology chosen during development. Even when more efficient or maintainable technologies come out, usually we have to wait to modernize for some time until we can justify the expense to the business side.

However, because TypeScript is JavaScript, any existing JavaScript files you have are compatible with any new file you add. If you have a current JavaScript application, I encourage you to consider the advantages that TypeScript has to offer you and your enterprise application.


About the Author
Clayton Terry

Clayton Terry

I am a developer who loves to find new ways to do old ideas. I pride myself on innovative solutions and not being tied to a specific technology stack.


Share this Post

Leave a Reply