Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exactOptionalPropertyTypes option #564

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ See [server demo](example) and [browser demo](https://github.com/bcherny/json-sc
| 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)? |
| inferStringEnumKeysFromValues | boolean | `false` | Create enums from JSON enums with eponymous keys |
| 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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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 @@ -52,6 +52,12 @@ export interface Options {
* Create enums from JSON enums with eponymous keys
*/
inferStringEnumKeysFromValues: 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 @@ -100,6 +106,7 @@ export const DEFAULT_OPTIONS: Options = {
declareExternallyReferenced: true,
enableConstEnums: true,
inferStringEnumKeysFromValues: false,
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 @@ -2326,6 +2326,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,
}