Skip to content

Commit 36f1399

Browse files
committed
Add exactOptionalPropertyTypes option
1 parent 3bb5d2b commit 36f1399

File tree

7 files changed

+71
-1
lines changed

7 files changed

+71
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ See [server demo](example) and [browser demo](https://github.com/bcherny/json-sc
8888
| cwd | string | `process.cwd()` | Root directory for resolving [`$ref`](https://tools.ietf.org/id/draft-pbryan-zyp-json-ref-03.html)s |
8989
| declareExternallyReferenced | boolean | `true` | Declare external schemas referenced via `$ref`? |
9090
| enableConstEnums | boolean | `true` | Prepend enums with [`const`](https://www.typescriptlang.org/docs/handbook/enums.html#computed-and-constant-members)? |
91+
| exactOptionalPropertyTypes | boolean | `false` | Append all optional property signatures with `\| undefined` so that they are strictly typed in accordance with TypeScript's [`exactOptionalPropertyTypes`](https://www.typescriptlang.org/tsconfig#exactOptionalPropertyTypes) option. |
9192
| format | boolean | `true` | Format code? Set this to `false` to improve performance. |
9293
| ignoreMinAndMaxItems | boolean | `false` | Ignore maxItems and minItems for `array` types, preventing tuples being generated. |
9394
| maxItems | number | `20` | Maximum number of unioned tuples to emit when representing bounded-size array types, before falling back to emitting unbounded arrays. Increase this to improve precision of emitted types, decrease it to improve performance, or set it to `-1` to ignore `maxItems`.

src/cli.ts

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ main(
2121
'additionalProperties',
2222
'declareExternallyReferenced',
2323
'enableConstEnums',
24+
'exactOptionalPropertyTypes',
2425
'format',
2526
'ignoreMinAndMaxItems',
2627
'strictIndexSignatures',

src/generator.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@ function generateInterface(ast: TInterface, options: Options): string {
308308
escapeKeyName(keyName) +
309309
(isRequired ? '' : '?') +
310310
': ' +
311-
type,
311+
type +
312+
(!isRequired && options.exactOptionalPropertyTypes ? ' | undefined' : ''),
312313
)
313314
.join('\n') +
314315
'\n' +

src/index.ts

+7
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ export interface Options {
4343
* Prepend enums with [`const`](https://www.typescriptlang.org/docs/handbook/enums.html#computed-and-constant-members)?
4444
*/
4545
enableConstEnums: boolean
46+
/**
47+
* Append all optional property signatures with `| undefined` so that they are strictly typed in accordance with
48+
* TypeScript's [`exactOptionalPropertyTypes`](https://www.typescriptlang.org/tsconfig#exactOptionalPropertyTypes)
49+
* option.
50+
*/
51+
exactOptionalPropertyTypes: boolean
4652
/**
4753
* Format code? Set this to `false` to improve performance.
4854
*/
@@ -90,6 +96,7 @@ export const DEFAULT_OPTIONS: Options = {
9096
cwd: process.cwd(),
9197
declareExternallyReferenced: true,
9298
enableConstEnums: true,
99+
exactOptionalPropertyTypes: false,
93100
format: true,
94101
ignoreMinAndMaxItems: false,
95102
maxItems: 20,

test/__snapshots__/test/test.ts.md

+27
Original file line numberDiff line numberDiff line change
@@ -2193,6 +2193,33 @@ Generated by [AVA](https://avajs.dev).
21932193
}␊
21942194
`
21952195

2196+
## options.exactOptionalPropertyTypes.js
2197+
2198+
> Expected output to match snapshot for e2e test: options.exactOptionalPropertyTypes.js
2199+
2200+
`/* eslint-disable */␊
2201+
/**␊
2202+
* This file was automatically generated by json-schema-to-typescript.␊
2203+
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,␊
2204+
* and run json-schema-to-typescript to regenerate this file.␊
2205+
*/␊
2206+
2207+
export interface ExactOptionalPropertyTypes {␊
2208+
maybe?: string | undefined;␊
2209+
complex?:␊
2210+
| {␊
2211+
maybe?: string | undefined;␊
2212+
[k: string]: Leaf;␊
2213+
}␊
2214+
| undefined;␊
2215+
[k: string]: string;␊
2216+
}␊
2217+
export interface Leaf {␊
2218+
maybe?: string | undefined;␊
2219+
[k: string]: unknown;␊
2220+
}␊
2221+
`
2222+
21962223
## options.format.js
21972224

21982225
> Expected output to match snapshot for e2e test: options.format.js

test/__snapshots__/test/test.ts.snap

-1.08 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export const input = {
2+
title: 'ExactOptionalPropertyTypes',
3+
type: 'object',
4+
properties: {
5+
maybe: {
6+
type: 'string',
7+
},
8+
complex: {
9+
type: 'object',
10+
properties: {
11+
maybe: {
12+
type: 'string',
13+
},
14+
},
15+
additionalProperties: {
16+
title: 'Leaf',
17+
type: 'object',
18+
properties: {
19+
maybe: {
20+
type: 'string',
21+
},
22+
},
23+
},
24+
},
25+
},
26+
additionalProperties: {
27+
type: 'string',
28+
}
29+
}
30+
31+
export const options = {
32+
exactOptionalPropertyTypes: true,
33+
}

0 commit comments

Comments
 (0)