Intent.Modules.Common.TypeScript module provides comprehensive support for generating TypeScript and JavaScript code, with support for modern ES6+ features, module systems, and NPM package management.
Template Base Class
All TypeScript templates inherit fromTypeScriptTemplateBase<TModel>, which provides:
- Automatic import management: ES6 imports with named and default exports
- NPM dependency management: Register package dependencies for package.json
- Type resolution: Resolves types from other templates with relative paths
- Module path calculation: Automatically computes relative import paths
Basic Template Structure
Key Properties and Methods
TypeScript Code Builder (TypescriptFile)
TheTypescriptFile builder provides a fluent API for constructing TypeScript code:
Building Classes
Interfaces
Type Aliases and Enums
- Type Aliases
- Enums
- Union Types
Import Statements
TypeScript’s import system is flexible and powerful:Modern TypeScript Features
Decorators
Generics
Advanced Types
Framework-Specific Features
- Angular
- React
- Node.js/Express
NPM Package Management
Intent can automatically manage your package.json dependencies:File Configuration
Best Practices
Use Strict TypeScript
Use Strict TypeScript
Generate code that works with
strict: true in tsconfig.json:- Use explicit types everywhere
- Handle null and undefined properly
- Use
readonlywhere appropriate
Follow TypeScript Conventions
Follow TypeScript Conventions
- Use PascalCase for classes and interfaces (User, IUserRepository)
- Use camelCase for properties and methods (firstName, getUserById)
- Prefer interfaces over type aliases for object shapes
- Use enum for discrete values
Module Organization
Module Organization
- One class/interface per file (with rare exceptions)
- Use index.ts to create public APIs
- Keep related types together (User, UserDto, IUserRepository)
Import Path Strategy
Import Path Strategy
Use the
GetRelativePath() method for imports between generated files. This ensures correct relative paths regardless of folder structure.Comparing with C# and Java
| Feature | TypeScript | C# | Java |
|---|---|---|---|
| Module System | ES6 imports/exports | Namespace + using | Package + import |
| Type System | Structural typing | Nominal typing | Nominal typing |
| Nullability | string | null or string? | string? (C# 8+) | @Nullable (annotations) |
| Properties | Class fields or getters | Properties with get/set | Getter/setter methods |
| Package Manager | npm/yarn | NuGet | Maven/Gradle |
See Also
C# Support
Generate C# code for .NET projects
Java Support
Generate Java code for JVM projects
Template Basics
Learn about Intent templates
Type Resolution
Understanding cross-template type references