Installations
npm install ts-to-json
Developer Guide
Typescript
Yes
Module System
CommonJS
Min. Node Version
>=10.0.0
Score
76.7
Supply Chain
99.1
Quality
74.6
Maintenance
100
Vulnerability
99.3
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (99.57%)
JavaScript (0.43%)
Developer
Cyrus-d
Download Statistics
Total Downloads
161,546
Last Day
99
Last Week
439
Last Month
1,950
Last Year
47,748
GitHub Statistics
11 Stars
2,033 Commits
2 Forks
3 Watching
27 Branches
1 Contributors
Bundle Size
3.03 MB
Minified
852.12 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.8.3
Package Id
ts-to-json@1.8.3
Unpacked Size
1.03 MB
Size
221.86 kB
File Count
898
Publised On
13 Jun 2023
Total Downloads
Cumulative downloads
Total Downloads
161,546
Last day
-31.7%
99
Compared to previous day
Last week
-34%
439
Compared to previous week
Last month
-14.5%
1,950
Compared to previous month
Last year
33.8%
47,748
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
9
Dev Dependencies
26
ts-to-json
Extended version of https://github.com/xiag-ag/typescript-to-json-schema.
Inspired by YousefED/typescript-json-schema
. Here's the differences list:
- this implementation avoids the use of
typeChecker.getTypeAtLocation()
(so probably it keeps correct type aliases) - processing AST and formatting JSON schema have been split into two independent steps
- not exported types, interfaces, enums are not exposed in the
definitions
section in the JSON schema
Contributors
This project is made possible by a community of contributors. We welcome contributions of any kind (issues, code, documentation, examples, tests,...). Please read our code of conduct.
CLI Usage
1npm install --save ts-to-json 2./node_modules/.bin/ts-to-json --path 'my/project/**.*.ts' --type 'My.Type.Full.Name'
Programmatic Usage
1// main.js 2 3const tsj = require("ts-to-json"); 4const fs = require("fs"); 5 6const config = { 7 path: "path/to/source/file", 8 tsconfig: "path/to/tsconfig.json", 9 type: "*", // Or <type-name> if you want to generate schema for that one type only 10 expose: "export", 11 jsDoc: "extended", 12 topRef: true, 13}; 14 15const output_path = "path/to/output/file"; 16 17const schema = tsj.createGenerator(config).createSchema(config.type); 18const schemaString = JSON.stringify(schema, null, 2); 19fs.writeFile(output_path, schemaString, (err) => { 20 if (err) throw err; 21});
Run the schema generator via node main.js
.
Options
-p, --path 'index.ts'
The path to the TypeScript source file. If this is not provided, the type will be searched in the project specified in the `.tsconfig`.
-t, --type 'My.Type.Full.Name'
The type the generated schema will represent. If omitted, the generated schema will contain all
types found in the files matching path. The same is true if '*' is specified.
-e, --expose <all|none|export>
all: Create shared $ref definitions for all types.
none: Do not create shared $ref definitions.
export (default): Create shared $ref definitions only for exported types.
-f, --tsconfig 'my/project/tsconfig.json'
Use a custom tsconfig file for processing typescript (see https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) instead of the default:
{
"compilerOptions": {
"noEmit": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "ES5",
"module": "CommonJS",
"strictNullChecks": false,
}
}
-j, --jsDoc <extended|none|basic>
none: Do not use JsDoc annotations.
basic: Read JsDoc annotations to provide schema properties.
extended (default): Also read @nullable, and @asType annotations.
--unstable
Do not sort properties.
--strict-tuples
Do not allow additional items on tuples.
--no-top-ref
Do not create a top-level $ref definition.
--no-type-check
Skip type checks for better performance.
--no-ref-encode
Do not encode references. According to the standard, references must be valid URIs but some tools do not support encoded references.
--validationKeywords
Provide additional validation keywords to include.
-o, --out
Specify the output file path. Without this option, the generator logs the response in the console.
Current state
interface
typesenum
typesunion
,tuple
,type[]
typesstring
,boolean
,number
types"value"
,123
,true
,false
,null
,undefined
literals- type aliases
- generics
typeof
keyof
- conditional types
Run locally
yarn --silent run run --path 'test/valid-data/type-mapped-array/*.ts' --type 'MyObject'
Debug
yarn --silent run debug --path 'test/valid-data/type-mapped-array/*.ts' --type 'MyObject'
And connect via the debugger protocol.
AST Explorer is amazing for developers of this tool!
Other options
Currently this options are only available when using this package programmatically.
/**
* Node that enabling this option can have side effects!
* If two file using same interface/type name and linking them together, the following error will be thrown:
* 'Type "Props" has multiple definitions.'
* It is good practice to not have same name for the type in the working file, so changing the type name will solve the problem.
* however if you like to suppress the error you can use this options.
*/
ignoreMultipleDefinitions?: boolean;
/**
* If returns false the node type name will be used instead of parsing node and its children.
* e.g. HTMLElement will stay HTMLElement
* This option has priority over other limit options.
*/
shouldParseNode?: (node: Node) => boolean;
/**
* Use this option when parser unable to parse specific type and throws error.
* This option force the parser identified type as unknown type.
*/
handleUnknownTypes?: boolean;
/**
* When unknown type detected, the node info will be displayed.
*/
showUnknownTypeInfo?: boolean;
/**
* Types located in the file wont be processed, instead name of type will be returned.
* e.g. HTMLElement is located in lib.dom.d.ts file, hence the HTMLElement will be the type
*/
skipParseFiles?: string[];
/**
* Type names within the list wont be proceeded instead the name of the type will be returned.
* e.g. HTMLElement will stay HTMLElement
*/
skipParseTypes?: string[];
/**
* Names within the list must be processed even if its in skipParseFiles or skipParseTypes list
*/
forceToParseTypes?: string[];
/*
If set to true types like any, unknown will be set
*/
allowArbitraryDataTypes?: boolean;
The following option can only be used when
expose
set tonone
andtype
option provided.
/**
* Will exclude props names specified in the list from root. accepts path eg. a.b
*/
excludeRootProps?: string[];
/**
* Will exclude prop names specified in the list regardless of root and childrens.
*/
excludeProps?: string[];
/**
* Will only generate schema for the property names included in the list.
* excludeRootProps option has no effect when using this option.
*/
includeProps?: string[];
/**
* This option has priority over includeProps and excludeRootProps options.
*/
maxDepth?: number;
/**
* Limits the maximum depth of function parameters
*/
funcParamMaxDepth?: number;
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
7 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-4q6p-r6v2-jvc5
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/build.yml:1
- Warn: no topLevel permission defined: .github/workflows/test.yml:1
- Info: no jobLevel write permissions found
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
project is archived
Details
- Warn: Repository is archived.
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build.yml:12: update your workflow using https://app.stepsecurity.io/secureworkflow/ccpu/ts-to-json/build.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build.yml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/ccpu/ts-to-json/build.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:23: update your workflow using https://app.stepsecurity.io/secureworkflow/ccpu/ts-to-json/test.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:25: update your workflow using https://app.stepsecurity.io/secureworkflow/ccpu/ts-to-json/test.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/test.yml:42: update your workflow using https://app.stepsecurity.io/secureworkflow/ccpu/ts-to-json/test.yml/master?enable=pin
- Info: 0 out of 4 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 1 are checked with a SAST tool
Score
2.8
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to ts-to-json
json-schema-to-ts
Infer typescript types from your JSON schemas!
copy-anything
An optimised way to copy'ing an object. A small and simple integration
json-to-ts
Convert json object to typescript interfaces
@fastify/type-provider-json-schema-to-ts
A Type Provider for json-schema-to-ts over Fastify