Skip to content

Commit cce9f1e

Browse files
committed
bring in functionality from bcherny#673
1 parent 43ba08b commit cce9f1e

File tree

7 files changed

+342
-277
lines changed

7 files changed

+342
-277
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ See [server demo](example) and [browser demo](https://github.com/bcherny/json-sc
141141
| inferStringEnumKeysFromValues | boolean | `false` | Create enums from JSON enums with eponymous keys |
142142
| format | boolean | `true` | Format code? Set this to `false` to improve performance. |
143143
| ignoreMinAndMaxItems | boolean | `false` | Ignore maxItems and minItems for `array` types, preventing tuples being generated. |
144-
| 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`.
144+
| 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`. |
145+
| removeOptionalIfDefaultExists | boolean | `false` | Remove the optional modifier when a property has a default value. |
145146
| strictIndexSignatures | boolean | `false` | Append all index signatures with `\| undefined` so that they are strictly typed. |
146147
| style | object | `{ bracketSpacing: false, printWidth: 120, semi: true, singleQuote: false, tabWidth: 2, trailingComma: 'none', useTabs: false }` | A [Prettier](https://prettier.io/docs/en/options.html) configuration |
147148
| unknownAny | boolean | `true` | Use `unknown` instead of `any` where possible |

src/cli.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ main(
2020
'enableConstEnums',
2121
'format',
2222
'ignoreMinAndMaxItems',
23+
'removeOptionalIfDefaultExists',
2324
'strictIndexSignatures',
2425
'unknownAny',
2526
'unreachableDefinitions',
@@ -190,6 +191,8 @@ Boolean values can be set to false using the 'no-' prefix.
190191
array types, before falling back to emitting unbounded arrays. Increase
191192
this to improve precision of emitted types, decrease it to improve
192193
performance, or set it to -1 to ignore minItems and maxItems.
194+
--removeOptionalIfDefaultExists
195+
Remove the optional modifier when a property has a default value
193196
--style.XXX=YYY
194197
Prettier configuration
195198
--unknownAny

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ export interface Options {
6767
* `minItems` and `maxItems`.
6868
*/
6969
maxItems: number
70+
/**
71+
* Remove the optional modifier when a property has a default value.
72+
*/
73+
removeOptionalIfDefaultExists: boolean
7074
/**
7175
* Append all index signatures with `| undefined` so that they are strictly typed.
7276
*
@@ -103,6 +107,7 @@ export const DEFAULT_OPTIONS: Options = {
103107
format: true,
104108
ignoreMinAndMaxItems: false,
105109
maxItems: 20,
110+
removeOptionalIfDefaultExists: false,
106111
strictIndexSignatures: false,
107112
style: {
108113
bracketSpacing: false,

src/parser.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ function parseSchema(
383383
let asts: TInterfaceParam[] = map(schema.properties, (value, key: string) => ({
384384
ast: parse(value, options, key, processed, usedNames),
385385
isPatternProperty: false,
386-
isRequired: includes(schema.required || [], key),
386+
isRequired: includes(schema.required || [], key) || (options.removeOptionalIfDefaultExists && 'default' in value),
387387
isUnreachableDefinition: false,
388388
keyName: key,
389389
}))
@@ -404,7 +404,10 @@ via the \`patternProperty\` "${key.replace('*/', '*\\/')}".`
404404
return {
405405
ast,
406406
isPatternProperty: !singlePatternProperty,
407-
isRequired: singlePatternProperty || includes(schema.required || [], key),
407+
isRequired:
408+
singlePatternProperty ||
409+
includes(schema.required || [], key) ||
410+
(options.removeOptionalIfDefaultExists && 'default' in value),
408411
isUnreachableDefinition: false,
409412
keyName: singlePatternProperty ? '[k: string]' : key,
410413
}
@@ -422,7 +425,8 @@ via the \`definition\` "${key}".`
422425
return {
423426
ast,
424427
isPatternProperty: false,
425-
isRequired: includes(schema.required || [], key),
428+
isRequired:
429+
includes(schema.required || [], key) || (options.removeOptionalIfDefaultExists && 'default' in value),
426430
isUnreachableDefinition: true,
427431
keyName: key,
428432
}

0 commit comments

Comments
 (0)