0
# Program Management
1
2
Create and manage TypeScript programs with custom compiler options, file resolution, and configuration loading for schema generation.
3
4
## Capabilities
5
6
### Program Factory Function
7
8
Creates TypeScript program instances with proper configuration for schema generation.
9
10
```typescript { .api }
11
/**
12
* Create a TypeScript program from configuration
13
* @param config - Configuration object with path and tsconfig options
14
* @returns Configured TypeScript Program instance
15
*/
16
function createProgram(config: CompletedConfig): ts.Program;
17
```
18
19
**Usage Examples:**
20
21
```typescript
22
import { createProgram } from "ts-json-schema-generator";
23
24
// Basic program creation
25
const config = {
26
path: "src/**/*.ts",
27
tsconfig: "./tsconfig.json"
28
};
29
const program = createProgram(config);
30
31
// Program with custom options
32
const customConfig = {
33
path: "src/api.ts",
34
skipTypeCheck: true
35
};
36
const fastProgram = createProgram(customConfig);
37
```
38
39
### TSConfig File Loading
40
41
Internal utilities for loading and parsing TypeScript configuration files (not exposed in public API).
42
43
**Default Compiler Options:**
44
45
When no tsconfig is specified, the following default options are used:
46
47
```typescript
48
const defaultOptions: ts.CompilerOptions = {
49
noEmit: true,
50
emitDecoratorMetadata: true,
51
experimentalDecorators: true,
52
target: ts.ScriptTarget.ES5,
53
module: ts.ModuleKind.CommonJS,
54
strictNullChecks: false
55
};
56
```
57
58
### File Resolution
59
60
Handles glob patterns and file discovery for TypeScript source files.
61
62
**Glob Pattern Support:**
63
64
```typescript
65
// Single file
66
const singleFileConfig = { path: "src/types.ts" };
67
68
// Multiple files with glob
69
const globConfig = { path: "src/**/*.ts" };
70
71
// Exclude patterns (use standard glob syntax)
72
const excludeConfig = { path: "src/**/*.ts" };
73
// Note: Exclusions handled by glob library, not by this package
74
```
75
76
### Type Checking Control
77
78
Control TypeScript type checking during program creation for performance optimization.
79
80
```typescript { .api }
81
interface Config {
82
/** Skip type checks to improve performance */
83
skipTypeCheck?: boolean;
84
}
85
```
86
87
**Type Checking Behavior:**
88
89
```typescript
90
// Default behavior - includes type checking
91
const config = {
92
path: "src/api.ts",
93
skipTypeCheck: false // This is the default
94
};
95
96
// Skip type checking for faster generation
97
const fastConfig = {
98
path: "src/api.ts",
99
skipTypeCheck: true
100
};
101
```
102
103
When `skipTypeCheck` is false (default), the program will:
104
1. Run full TypeScript diagnostics
105
2. Throw `BuildError` if type errors are found
106
3. Ensure type safety during schema generation
107
108
When `skipTypeCheck` is true:
109
1. Skip pre-emit diagnostics
110
2. Generate schemas even with type errors
111
3. Significantly faster for large codebases
112
113
### Error Handling
114
115
Program creation can throw errors for various configuration issues. The `BuildError` class is available from the main export.
116
117
**Common Error Scenarios:**
118
119
```typescript
120
import { createProgram, BuildError } from "ts-json-schema-generator";
121
122
try {
123
const program = createProgram({
124
path: "nonexistent.ts",
125
tsconfig: "./tsconfig.json"
126
});
127
} catch (error) {
128
if (error instanceof BuildError) {
129
console.error("Build failed:", error.format());
130
// Handle TypeScript compilation errors
131
}
132
}
133
```
134
135
**Error Types:**
136
- **Cannot read config file**: Invalid or missing tsconfig.json
137
- **Invalid parsed config file**: Malformed tsconfig.json content
138
- **No input files**: No files found matching the path pattern
139
- **Type check error**: TypeScript compilation errors (when skipTypeCheck is false)
140
141
### Configuration Integration
142
143
Program creation integrates with the main configuration system.
144
145
```typescript { .api }
146
interface Config {
147
/** Source file path or glob pattern */
148
path?: string;
149
/** Custom tsconfig.json path */
150
tsconfig?: string;
151
/** Skip type checks to improve performance */
152
skipTypeCheck?: boolean;
153
}
154
```
155
156
**Path Resolution Priority:**
157
1. Files matching `config.path` glob pattern (if provided)
158
2. Files from `tsconfig.json` fileNames (if no path provided)
159
3. Error if no files found from either source
160
161
**TSConfig Resolution:**
162
1. Use `config.tsconfig` path if provided
163
2. Search for tsconfig.json in current and parent directories
164
3. Use default compiler options if no tsconfig found