Skip to content
This repository was archived by the owner on Nov 14, 2022. It is now read-only.

Commit 8f0b1bc

Browse files
dl748Jeremy Walton
and
Jeremy Walton
authored
Adding ability to convert any's into unknown's (bcherny#281)
* Adding ability to convert any's into unknown's * Fix snapshots * Updating README and printHelp Co-authored-by: Jeremy Walton <[email protected]>
1 parent 25f26c1 commit 8f0b1bc

File tree

7 files changed

+46
-9
lines changed

7 files changed

+46
-9
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ See [server demo](example) and [browser demo](https://github.com/bcherny/json-sc
9292
| unreachableDefinitions | boolean | `false` | Generates code for `definitions` that aren't referenced by the schema. |
9393
| strictIndexSignatures | boolean | `false` | Append all index signatures with `| undefined` so that they are strictly typed. |
9494
| $refOptions | object | `{}` | [$RefParser](https://github.com/BigstickCarpet/json-schema-ref-parser) Options, used when resolving `$ref`s |
95+
| unknownAny | boolean | `false` | Converts all `any` types into `unknown`'s |
9596
## CLI
9697

9798
A CLI utility is provided with this package.

src/cli.ts

+2
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ Boolean values can be set to false using the 'no-' prefix.
156156
Prettier configuration
157157
--unreachableDefinitions
158158
Generates code for definitions that aren't referenced by the schema
159+
--unknownAny
160+
Output unknown type instead of any type
159161
`
160162
)
161163
}

src/generator.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ function generateRawType(ast: AST, options: Options): string {
179179

180180
switch (ast.type) {
181181
case 'ANY':
182-
return 'any'
182+
return options.unknownAny ? 'unknown' : 'any'
183183
case 'ARRAY':
184184
return (() => {
185185
const type = generateType(ast.params, options)

src/index.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ export interface Options {
5050
* Generate code for `definitions` that aren't referenced by the schema?
5151
*/
5252
unreachableDefinitions: boolean
53+
/**
54+
* Generate unknown type instead of any
55+
*/
56+
unknownAny: boolean
5357
/**
5458
* [$RefParser](https://github.com/BigstickCarpet/json-schema-ref-parser) Options, used when resolving `$ref`s
5559
*/
@@ -78,7 +82,8 @@ export const DEFAULT_OPTIONS: Options = {
7882
trailingComma: 'none',
7983
useTabs: false
8084
},
81-
unreachableDefinitions: false
85+
unreachableDefinitions: false,
86+
unknownAny: false
8287
}
8388

8489
export function compileFromFile(filename: string, options: Partial<Options> = DEFAULT_OPTIONS): Promise<string> {

test/__snapshots__/test/test.ts.md

+32-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,31 @@ The actual snapshot is saved in `test.ts.snap`.
44

55
Generated by [AVA](https://avajs.dev).
66

7+
## --unknownAny
8+
9+
> Snapshot 1
10+
11+
`/* tslint:disable */␊
12+
/**␊
13+
* This file was automatically generated by json-schema-to-typescript.␊
14+
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,␊
15+
* and run json-schema-to-typescript to regenerate this file.␊
16+
*/␊
17+
18+
export interface ExampleSchema {␊
19+
firstName: string;␊
20+
lastName: string;␊
21+
/**␊
22+
* Age in years␊
23+
*/␊
24+
age?: number;␊
25+
height?: number;␊
26+
favoriteFoods?: unknown[];␊
27+
likesDogs?: boolean;␊
28+
[k: string]: unknown;␊
29+
}␊
30+
`
31+
732
## Add empty `required` property if none is defined
833

934
> Snapshot 1
@@ -2016,10 +2041,9 @@ Generated by [AVA](https://avajs.dev).
20162041
* and run json-schema-to-typescript to regenerate this file.␊
20172042
*/␊
20182043
2019-
export interface BSchema {␊
2020-
x?: string;␊
2021-
y: number;␊
2022-
[k: string]: any;␊
2044+
export interface ASchema {␊
2045+
f: string;␊
2046+
g?: number;␊
20232047
}␊
20242048
/* tslint:disable */␊
20252049
/**␊
@@ -2028,9 +2052,10 @@ Generated by [AVA](https://avajs.dev).
20282052
* and run json-schema-to-typescript to regenerate this file.␊
20292053
*/␊
20302054
2031-
export interface ASchema {␊
2032-
f: string;␊
2033-
g?: number;␊
2055+
export interface BSchema {␊
2056+
x?: string;␊
2057+
y: number;␊
2058+
[k: string]: any;␊
20342059
}␊
20352060
`
20362061

test/__snapshots__/test/test.ts.snap

42 Bytes
Binary file not shown.

test/testCLI.ts

+4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ export function run() {
7676
unlinkSync('./ReferencedType.d.ts')
7777
})
7878

79+
test('--unknownAny', t => {
80+
t.snapshot(execSync('node dist/src/cli.js --unknownAny --input ./test/resources/ReferencedType.json').toString())
81+
})
82+
7983
test('files in (-i), files out (-o)', t => {
8084
execSync("node dist/src/cli.js -i './test/resources/MultiSchema/**/*.json' -o ./test/resources/MultiSchema/out")
8185

0 commit comments

Comments
 (0)