|
| 1 | +# TypeScript Code Generation Project |
| 2 | + |
| 3 | +This directory contains a TypeScript project that demonstrates code generation using `ion-cli` with TypeScript as the target language. |
| 4 | + |
| 5 | +## Project Structure |
| 6 | + |
| 7 | +``` |
| 8 | +typescript/ |
| 9 | +├── src/ |
| 10 | +│ ├── models/ # Generated TypeScript interfaces |
| 11 | +│ ├── serializers/ # Ion serialization code |
| 12 | +│ └── validators/ # Schema validation code |
| 13 | +├── tests/ |
| 14 | +│ ├── good/ # Valid test cases |
| 15 | +│ └── bad/ # Invalid test cases |
| 16 | +├── package.json |
| 17 | +└── tsconfig.json |
| 18 | +``` |
| 19 | + |
| 20 | +## Build Process |
| 21 | + |
| 22 | +The TypeScript code generation is integrated into the build process using npm scripts. The build process: |
| 23 | + |
| 24 | +1. Checks for `ion-cli` availability |
| 25 | +2. Generates TypeScript code from schemas |
| 26 | +3. Compiles TypeScript to JavaScript |
| 27 | +4. Runs tests |
| 28 | + |
| 29 | +### NPM Scripts |
| 30 | + |
| 31 | +```json |
| 32 | +{ |
| 33 | + "scripts": { |
| 34 | + "generate": "ion-cli generate -l typescript -d ../../schema -o ./src/models", |
| 35 | + "build": "tsc", |
| 36 | + "test": "jest", |
| 37 | + "clean": "rm -rf ./src/models/*" |
| 38 | + } |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +### Environment Setup |
| 43 | + |
| 44 | +1. Install ion-cli: |
| 45 | + ```bash |
| 46 | + brew install ion-cli |
| 47 | + # or |
| 48 | + cargo install ion-cli |
| 49 | + ``` |
| 50 | + |
| 51 | +2. Set up environment: |
| 52 | + ```bash |
| 53 | + export ION_CLI=/path/to/ion-cli # Optional, defaults to 'ion' |
| 54 | + ``` |
| 55 | + |
| 56 | +## Testing |
| 57 | + |
| 58 | +The project includes comprehensive tests for the generated code: |
| 59 | + |
| 60 | +### Unit Tests |
| 61 | +- Type guard validation |
| 62 | +- Serialization/deserialization |
| 63 | +- Null value handling |
| 64 | +- Type annotation preservation |
| 65 | + |
| 66 | +### Integration Tests |
| 67 | +- Roundtrip testing with good/bad inputs |
| 68 | +- Schema validation |
| 69 | +- Error handling |
| 70 | + |
| 71 | +### Running Tests |
| 72 | + |
| 73 | +```bash |
| 74 | +npm test |
| 75 | +``` |
| 76 | + |
| 77 | +## Type System |
| 78 | + |
| 79 | +The generated TypeScript code follows these principles: |
| 80 | + |
| 81 | +1. **Null Safety** |
| 82 | + - Explicit null handling |
| 83 | + - Optional type support |
| 84 | + - Undefined vs null distinction |
| 85 | + |
| 86 | +2. **Type Guards** |
| 87 | + - Runtime type checking |
| 88 | + - Custom validation rules |
| 89 | + - Schema constraint validation |
| 90 | + |
| 91 | +3. **Serialization** |
| 92 | + - Binary format support |
| 93 | + - Text format support |
| 94 | + - Type annotation preservation |
| 95 | + |
| 96 | +## Ion Type Mappings |
| 97 | + |
| 98 | +| Ion Type | TypeScript Type | |
| 99 | +|----------|----------------| |
| 100 | +| null | null | |
| 101 | +| bool | boolean | |
| 102 | +| int | number/bigint | |
| 103 | +| float | number | |
| 104 | +| decimal | Decimal | |
| 105 | +| timestamp| Date | |
| 106 | +| string | string | |
| 107 | +| symbol | Symbol | |
| 108 | +| blob | Uint8Array | |
| 109 | +| clob | string | |
| 110 | +| struct | interface | |
| 111 | +| list | Array | |
| 112 | +| sexp | Array | |
| 113 | + |
| 114 | +## Error Handling |
| 115 | + |
| 116 | +The generated code includes comprehensive error handling: |
| 117 | + |
| 118 | +- Schema validation errors |
| 119 | +- Type conversion errors |
| 120 | +- Serialization errors |
| 121 | +- Runtime validation errors |
0 commit comments