66 | Converting the following JSON Schema:
67 |
68 |
69 | ```json
70 | {
71 | "definitions": {
72 | "User": {
73 | "type": "object",
74 | "title": "User type",
75 | "description": "This type holds the user information, such as name",
76 | "properties": { "name": { "type": "string", "title": "The real name" } },
77 | "required": [ "name" ]
78 | },
79 | "ChatLine": {
80 | "type": "object",
81 | "title": "A chat line",
82 | "properties": {
83 | "user": { "$ref": "#/definitions/User" },
84 | "line": { "type": "string" }
85 | },
86 | "required": [ "user", "line" ]
87 | }
88 | }
89 | }
90 | ```
91 |
92 |
93 |
94 |
95 |
160 | $ typeconv --help
161 |
162 |
163 | ```
164 | Usage: typeconv [options] file ...
165 |
166 | Options:
167 |
168 | -h, --help Print (this) help screen
169 | --version Print the program version
170 | -v, --verbose Verbose informational output (default: false)
171 | --dry-run Prepare and perform conversion, but write no output (default: false)
172 | --(no-)hidden Include hidden files, i.e. files in .gitignore,
173 | files beginning with '.' and the '.git' directory
174 | (default: true)
175 | -f, --from-type Type system to convert from
176 |
177 | Values:
178 | ts TypeScript
179 | jsc JSON Schema
180 | gql GraphQL
181 | oapi Open API
182 | st SureType
183 | ct core-types
184 |
185 | -t, --to-type Type system to convert to
186 |
187 | Values:
188 | ts TypeScript
189 | jsc JSON Schema
190 | gql GraphQL
191 | oapi Open API
192 | st SureType
193 | ct core-types
194 |
195 | --(no-)shortcut Shortcut conversion if possible (bypassing core-types).
196 | This is possible between SureType, JSON Schema and Open API
197 | to preserve all features which would otherwise be erased.
198 | (default: true)
199 | -o, --output-directory Output directory. Defaults to the same as the input files.
200 | -O, --output-extension Output filename extension to use.
201 | Defaults to 'ts'/'d.ts', 'json', 'yaml' and 'graphql'.
202 | Use '-' to not save a file, but output to stdout instead.
203 | --(no-)strip-annotations Removes all annotations (descriptions, comments, ...) (default: false)
204 | TypeScript
205 | --(no-)ts-declaration Output TypeScript declarations (default: false)
206 | --(no-)ts-disable-lint-header Output comments for disabling linting (default: true)
207 | --(no-)ts-descriptive-header Output the header comment (default: true)
208 | --(no-)ts-use-unknown Use 'unknown' type instead of 'any' (default: true)
209 | --ts-non-exported Strategy for non-exported types (default: include-if-referenced)
210 |
211 | Values:
212 | fail Fail conversion
213 | ignore Don't include non-exported types,
214 | even if referenced
215 | include Include non-exported types
216 | inline Don't include non-exported types,
217 | inline them if necessary.
218 | Will fail on cyclic types
219 | include-if-referenced Include non-exported types only if they
220 | are referenced from exported types
221 |
222 | --ts-namespaces Namespace strategy. (default: ignore)
223 |
224 | Values:
225 | ignore Ignore namespaces entirely (default).
226 | - When converting from TypeScript, types in namespaces
227 | aren't exported.
228 | - When converting to TypeScript, no attempt to
229 | reconstruct namespaces is performed.
230 | hoist When converting from TypeScript, hoist types inside
231 | namespaces to top-level, so that the types are
232 | included, but without their namespace.
233 | This can cause conflicts, in which case deeper
234 | declarations will be dropped in favor of more top-
235 | level declarations.
236 | In case of same-level (namespace depth) declarations
237 | with the same name, only one will be exported in a
238 | non-deterministic manner.
239 | dot When converting from TypeScript, join the namespaces
240 | and the exported type with a dot (.).
241 | When converting to TypeScript, try to reconstruct
242 | namespaces by splitting the name on dot (.).
243 | underscore When converting from TypeScript, join the namespaces
244 | and the exported type with an underscore (_).
245 | When converting to TypeScript, try to reconstruct
246 | namespaces by splitting the name on underscore (_).
247 | reconstruct-all When converting to TypeScript, try to reconstruct
248 | namespaces by splitting the name on both dot and
249 | underscore.
250 |
251 | GraphQL
252 | --gql-unsupported Method to use for unsupported types
253 |
254 | Values:
255 | ignore Ignore (skip) type
256 | warn Ignore type, but warn
257 | error Throw an error
258 |
259 | --gql-null-typename Custom type name to use for null
260 | Open API
261 | --oapi-format Output format for Open API (default: yaml)
262 |
263 | Values:
264 | json JSON
265 | yaml YAML ('yml' is also allowed)
266 |
267 | --oapi-title Open API title to use in output document.
268 | Defaults to the input filename.
269 | --oapi-version Open API document version to use in output document. (default: 1)
270 | SureType
271 | --st-ref-method SureType reference export method (default: provided)
272 |
273 | Values:
274 | no-refs Don't ref anything, inline all types
275 | provided Reference types that are explicitly exported
276 | ref-all Ref all provided types and those with names
277 |
278 | --st-missing-ref What to do when detecting an unresolvable reference (default: warn)
279 |
280 | Values:
281 | ignore Ignore; skip type or cast to any
282 | warn Same as 'ignore', but warn
283 | error Fail conversion
284 |
285 | --(no-)st-inline-types Inline pretty typescript types aside validator code (default: true)
286 | --(no-)st-export-type Export the deduced types (or the pretty types,
287 | depending on --st-inline-types)
288 | (default: true)
289 | --(no-)st-export-schema Export validator schemas (default: false)
290 | --(no-)st-export-validator Export regular validators (default: true)
291 | --(no-)st-export-ensurer Export 'ensurer' validators (default: true)
292 | --(no-)st-export-type-guard Export type guards (is* validators) (default: true)
293 | --(no-)st-use-unknown Use 'unknown' type instead of 'any' (default: true)
294 | --(no-)st-forward-schema Forward the JSON Schema, and create an untyped validator schema
295 | with the raw JSON Schema under the hood
296 | (default: false)
297 | ```
298 |
299 |
300 |
301 |
302 |
303 | ## As API
304 |
305 | To convert from one type system to another, you create a *reader* for the type system to convert **from** and a *writer* for the type system to convert **to**. The readers and writers for the different type systems have their own set of options. Some have no options (like the JSON Schema reader), some require options (like the Open API writer).
306 |
307 |
308 | ### makeConverter
309 |
310 | Making a converter is done using `makeConverter(reader, writer, options?)`, which takes a reader and a writer, and optionally options:
311 |
312 | #### **cwd** (string)
313 |
314 | The current working directory, only useful when working with files.
315 |
316 | #### **simplify** (boolean) (default true)
317 |
318 | When simplify is true, the converter will let core-types
319 | [*compress*](https://github.com/grantila/core-types#simplify) the
320 | types after having converted from {reader} format to core-types.
321 | This is usually recommended, but may cause some annotations (comments)
322 | to be dropped.
323 |
324 | #### **map** (ConvertMapFunction)
325 |
326 | Custom map function for transforming each type after it has been
327 | converted *from* the source type (and after it has been simplified),
328 | but before it's written to the target type system.
329 |
330 | Type: `(node: NamedType, index: number, array: ReadonlyArray