Currently, I am working on an ASP.NET project using .NET 4.6 and Visual Studio 2015. The business logic is primarily written in C#, but there is also a significant amount of client-side logic involved. To help organize my client-side code in a way similar to how I'm used to with C#, I decided to use TypeScript.
However, as I delved deeper into TypeScript, I realized that it differs significantly from C#. One major issue I encountered is that TypeScript does not support the simple class-per-file rule, which would allow me to include multiple classes or interfaces in a single file.
This limitation becomes problematic when defining ViewModel classes and initialization scripts for different pages where I need to utilize various models and utilities.
In my search for solutions, I came across two approaches:
A: Encapsulating each class within a namespace:
// Similar to C# using
import utils = App.Code.Utils;
namespace App.Code {
export class ZipCodeValidator {
public zipCode: string;
public validate(): boolean {
let stringValidator: utils.StringValidator = new utils.StringValidator();
// TODO validation
return true;
}
}
}
While initially satisfied with this solution, I soon found out that managing file order manually can become cumbersome, especially as the number of files increases due to the class-per-file rule.
B: Using Modules:
import { StringValidator } from "./Utils/StringValidator";
export default class ZipCodeValidator {
public zipCode: string;
public validate(): boolean {
let stringValidator: StringValidator = new StringValidator();
// TODO validation
return true;
}
}
Although using modules eliminates the need to manage file order manually, importing individual classes can be tedious. Additionally, separating source into different JS files based on directory structure seems challenging.
Ultimately, I am looking for a better approach to streamline my TypeScript development process. Can you suggest any tools, plugins, or techniques to facilitate this? I want to leverage TypeScript efficiently without resorting to manual work. Hopefully, the insights shared here will benefit others facing similar challenges.
Thank you.