@naturalcycles/common-type
Common Type interface and generator
Allows to generate JSON Schema from a set of Typescript files with
Interfaces (also Types and Enums).
Also, to generate Typescript files from JSON schemas (reverse).
Status: experimental! Everything is subject to change!
Example
Install common-type
as your devDependency:
yarn add -D @naturacycles/common-type
Define a commonType.cfg.js
in the root of you project:
module.exports = {
paths: ['src/types/*.ts'],
outputDir: 'src/schemas',
}
Config says to scan all *.ts
files in src/types
, parse them, generate JSON schemas, write them
into src/schemas
folder.
Similar projects
Develop
Typescript AST debugging: https://ts-ast-viewer.com/
Supported
- Schemas from:
- Interfaces
- Types
- Enums
- Classes
string
, number
, boolean
- Literal types, e.g
someString
, 15, true
null
typeobject
, array
tuple
enum
(string
and number
values)- optional/required properties
- Union types (
|
), Intersections (&
), extends
- jsdoc
@validationType
(e.g. integer
) - Type references, e.g.
p: Person
(where Person
is defined elsewhere, can be in another file) - Parsing a list of files (not necessary for all types to be in 1 file)
Partial
, Required
Record<A, B>
- Indexed properties (
{ [name: string]: string }
) StringMap
(aka Dictionary)- jsdoc tags:
validationType
to override type
, e.g. @validationType integer
- General:
deprecated
, readOnly
, writeOnly
, default
- String:
pattern
, format
, minLength
, maxLength
- Number:
multipleOf
, minimum
, exclusiveMinimum
, maximum
, exclusiveMaximum
- Object:
additionalProperties
, minProperties
, maxProperties
- Array:
minItems
, maxItems
, uniqueItems
- Schema composition:
if
, then
, else
, dependencies
, dependentRequired
,
dependentSchemas
Currently NOT supported
Omit
, Pick
- Generic interfaces, e.g.
interface MyType<T> { current: T, future: T }
typeof
keyof
- Conditional types