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.
All About TypeScript
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.
any in TypeScript unless otherwise specified in a .d.ts file.
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.
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.