Skip to content

Commit

Permalink
Add exactOptionalPropertyTypes option
Browse files Browse the repository at this point in the history
  • Loading branch information
rpnzl committed Dec 9, 2023
1 parent 3bb5d2b commit 36f1399
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ See [server demo](example) and [browser demo](https://github.com/bcherny/json-sc
| cwd | string | `process.cwd()` | Root directory for resolving [`$ref`](https://tools.ietf.org/id/draft-pbryan-zyp-json-ref-03.html)s |
| declareExternallyReferenced | boolean | `true` | Declare external schemas referenced via `$ref`? |
| enableConstEnums | boolean | `true` | Prepend enums with [`const`](https://www.typescriptlang.org/docs/handbook/enums.html#computed-and-constant-members)? |
| 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. |
| format | boolean | `true` | Format code? Set this to `false` to improve performance. |
| ignoreMinAndMaxItems | boolean | `false` | Ignore maxItems and minItems for `array` types, preventing tuples being generated. |
| 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`.
Expand Down
1 change: 1 addition & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ main(
'additionalProperties',
'declareExternallyReferenced',
'enableConstEnums',
'exactOptionalPropertyTypes',
'format',
'ignoreMinAndMaxItems',
'strictIndexSignatures',
Expand Down
3 changes: 2 additions & 1 deletion src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ function generateInterface(ast: TInterface, options: Options): string {
escapeKeyName(keyName) +
(isRequired ? '' : '?') +
': ' +
type,
type +
(!isRequired && options.exactOptionalPropertyTypes ? ' | undefined' : ''),
)
.join('\n') +
'\n' +
Expand Down
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ export interface Options {
* Prepend enums with [`const`](https://www.typescriptlang.org/docs/handbook/enums.html#computed-and-constant-members)?
*/
enableConstEnums: boolean
/**
* 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.
*/
exactOptionalPropertyTypes: boolean
/**
* Format code? Set this to `false` to improve performance.
*/
Expand Down Expand Up @@ -90,6 +96,7 @@ export const DEFAULT_OPTIONS: Options = {
cwd: process.cwd(),
declareExternallyReferenced: true,
enableConstEnums: true,
exactOptionalPropertyTypes: false,
format: true,
ignoreMinAndMaxItems: false,
maxItems: 20,
Expand Down
27 changes: 27 additions & 0 deletions test/__snapshots__/test/test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -2193,6 +2193,33 @@ Generated by [AVA](https://avajs.dev).
}␊
`

## options.exactOptionalPropertyTypes.js

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

`/* eslint-disable */␊
/**␊
* This file was automatically generated by json-schema-to-typescript.␊
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,␊
* and run json-schema-to-typescript to regenerate this file.␊
*/␊
export interface ExactOptionalPropertyTypes {␊
maybe?: string | undefined;␊
complex?:␊
| {␊
maybe?: string | undefined;␊
[k: string]: Leaf;␊
}␊
| undefined;␊
[k: string]: string;␊
}␊
export interface Leaf {␊
maybe?: string | undefined;␊
[k: string]: unknown;␊
}␊
`

## options.format.js

> Expected output to match snapshot for e2e test: options.format.js
Expand Down
Binary file modified test/__snapshots__/test/test.ts.snap
Binary file not shown.
33 changes: 33 additions & 0 deletions test/e2e/options.exactOptionalPropertyTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export const input = {
title: 'ExactOptionalPropertyTypes',
type: 'object',
properties: {
maybe: {
type: 'string',
},
complex: {
type: 'object',
properties: {
maybe: {
type: 'string',
},
},
additionalProperties: {
title: 'Leaf',
type: 'object',
properties: {
maybe: {
type: 'string',
},
},
},
},
},
additionalProperties: {
type: 'string',
}
}

export const options = {
exactOptionalPropertyTypes: true,
}

0 comments on commit 36f1399

Please sign in to comment.