|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: API Usage |
| 4 | +parent: API Reference |
| 5 | +nav_order: 1 |
| 6 | +--- |
| 7 | + |
| 8 | +# API Usage Guide |
| 9 | + |
| 10 | +This guide provides comprehensive examples and best practices for using the LuaTS API. |
| 11 | + |
| 12 | +## Basic Usage |
| 13 | + |
| 14 | +### Type Conversion |
| 15 | + |
| 16 | +```typescript |
| 17 | +import { generateTypes } from 'luats'; |
| 18 | + |
| 19 | +// Basic type conversion |
| 20 | +const tsCode = generateTypes(` |
| 21 | +type Vector3 = { |
| 22 | + x: number, |
| 23 | + y: number, |
| 24 | + z: number |
| 25 | +} |
| 26 | +`); |
| 27 | + |
| 28 | +console.log(tsCode); |
| 29 | +// Output: |
| 30 | +// interface Vector3 { |
| 31 | +// x: number; |
| 32 | +// y: number; |
| 33 | +// z: number; |
| 34 | +// } |
| 35 | +``` |
| 36 | + |
| 37 | +### Options |
| 38 | + |
| 39 | +```typescript |
| 40 | +import { generateTypes, GenerateOptions } from 'luats'; |
| 41 | + |
| 42 | +const options: GenerateOptions = { |
| 43 | + // Enable type inference for inline tables |
| 44 | + inferInlineTables: true, |
| 45 | + // Preserve original comments |
| 46 | + preserveComments: true, |
| 47 | + // Use JSDoc-style comments |
| 48 | + useJSDoc: true, |
| 49 | + // Convert table types to arrays when possible |
| 50 | + preferArrayTypes: true, |
| 51 | + // Handle type intersections |
| 52 | + handleIntersections: true |
| 53 | +}; |
| 54 | + |
| 55 | +const tsCode = generateTypes(` |
| 56 | +type User = { |
| 57 | + id: string, |
| 58 | + name: string, |
| 59 | + --! @deprecated |
| 60 | + legacyField: string? |
| 61 | +} |
| 62 | +`, options); |
| 63 | +``` |
| 64 | + |
| 65 | +## Advanced Usage |
| 66 | + |
| 67 | +### Type Merging |
| 68 | + |
| 69 | +```typescript |
| 70 | +import { generateTypes } from 'luats'; |
| 71 | + |
| 72 | +const tsCode = generateTypes(` |
| 73 | +type Base = { |
| 74 | + id: string |
| 75 | +} |
| 76 | +
|
| 77 | +type User = Base & { |
| 78 | + name: string |
| 79 | +} |
| 80 | +`); |
| 81 | + |
| 82 | +// Output: |
| 83 | +// interface Base { |
| 84 | +// id: string; |
| 85 | +// } |
| 86 | +// |
| 87 | +// interface User extends Base { |
| 88 | +// name: string; |
| 89 | +// } |
| 90 | +``` |
| 91 | + |
| 92 | +### Type Inference |
| 93 | + |
| 94 | +```typescript |
| 95 | +const tsCode = generateTypes(` |
| 96 | +local user = { |
| 97 | + id = "123", |
| 98 | + name = "John", |
| 99 | + age = 30 |
| 100 | +} |
| 101 | +`); |
| 102 | + |
| 103 | +// Output: |
| 104 | +// interface User { |
| 105 | +// id: string; |
| 106 | +// name: string; |
| 107 | +// age: number; |
| 108 | +// } |
| 109 | +``` |
| 110 | + |
| 111 | +### Function Types |
| 112 | + |
| 113 | +```typescript |
| 114 | +const tsCode = generateTypes(` |
| 115 | +type Callback = (value: number) => string |
| 116 | +
|
| 117 | +type User = { |
| 118 | + getName: () => string |
| 119 | +} |
| 120 | +`); |
| 121 | + |
| 122 | +// Output: |
| 123 | +// type Callback = (value: number) => string; |
| 124 | +// |
| 125 | +// interface User { |
| 126 | +// getName(): string; |
| 127 | +// } |
| 128 | +``` |
| 129 | + |
| 130 | +## Error Handling |
| 131 | + |
| 132 | +```typescript |
| 133 | +import { generateTypes } from 'luats'; |
| 134 | + |
| 135 | +try { |
| 136 | + const tsCode = generateTypes(` |
| 137 | + type Invalid = { |
| 138 | + -- Invalid syntax |
| 139 | + } |
| 140 | + `); |
| 141 | +} catch (error) { |
| 142 | + console.error(error.message); |
| 143 | + // Output: "Syntax error in type definition" |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +## Performance Considerations |
| 148 | + |
| 149 | +```typescript |
| 150 | +import { generateTypes } from 'luats'; |
| 151 | + |
| 152 | +// Process multiple files efficiently |
| 153 | +const files = [ |
| 154 | + 'types/user.luau', |
| 155 | + 'types/game.luau', |
| 156 | + 'types/ui.luau' |
| 157 | +]; |
| 158 | + |
| 159 | +// Process in batches |
| 160 | +const batchSize = 100; |
| 161 | +for (let i = 0; i < files.length; i += batchSize) { |
| 162 | + const batch = files.slice(i, i + batchSize); |
| 163 | + // Process batch... |
| 164 | +} |
| 165 | +``` |
| 166 | + |
| 167 | +## Best Practices |
| 168 | + |
| 169 | +1. **Use Type Aliases for Complex Types** |
| 170 | + ```typescript |
| 171 | + type ComplexType = { |
| 172 | + [key: string]: { |
| 173 | + value: any, |
| 174 | + metadata: { |
| 175 | + created: number, |
| 176 | + modified: number |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + ``` |
| 181 | +
|
| 182 | +2. **Document Types with JSDoc** |
| 183 | + ```typescript |
| 184 | + /** |
| 185 | + * Represents a user in the system |
| 186 | + * @property {string} id - Unique user identifier |
| 187 | + * @property {string} name - User's display name |
| 188 | + */ |
| 189 | + type User = { |
| 190 | + id: string, |
| 191 | + name: string |
| 192 | + } |
| 193 | + ``` |
| 194 | +
|
| 195 | +3. **Use Interfaces for Object Types** |
| 196 | + ```typescript |
| 197 | + interface User { |
| 198 | + id: string; |
| 199 | + name: string; |
| 200 | + } |
| 201 | + ``` |
| 202 | + |
| 203 | +4. **Prefer Union Types Over Any** |
| 204 | + ```typescript |
| 205 | + // Instead of: |
| 206 | + type Value = any; |
| 207 | + |
| 208 | + // Use: |
| 209 | + type Value = string | number | boolean; |
| 210 | + ``` |
| 211 | + |
| 212 | +## Common Pitfalls |
| 213 | + |
| 214 | +1. **Avoid Circular References** |
| 215 | + ```typescript |
| 216 | + // Bad: |
| 217 | + type Node = { |
| 218 | + children: Node[] |
| 219 | + } |
| 220 | + |
| 221 | + // Good: |
| 222 | + interface Node { |
| 223 | + children: Node[]; |
| 224 | + } |
| 225 | + ``` |
| 226 | + |
| 227 | +2. **Be Explicit with Optional Properties** |
| 228 | + ```typescript |
| 229 | + // Bad: |
| 230 | + type User = { |
| 231 | + name: string, |
| 232 | + age?: number |
| 233 | + } |
| 234 | + |
| 235 | + // Good: |
| 236 | + type User = { |
| 237 | + name: string, |
| 238 | + age?: number |
| 239 | + } |
| 240 | + ``` |
| 241 | +
|
| 242 | +3. **Use Type Guards** |
| 243 | + ```typescript |
| 244 | + type User = { |
| 245 | + type: 'user', |
| 246 | + name: string |
| 247 | + } |
| 248 | + |
| 249 | + type Admin = { |
| 250 | + type: 'admin', |
| 251 | + permissions: string[] |
| 252 | + } |
| 253 | + |
| 254 | + type Account = User | Admin; |
| 255 | + ``` |
0 commit comments