Skip to content
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ See [server demo](example) and [browser demo](https://github.com/bcherny/json-sc
| inferStringEnumKeysFromValues | boolean | `false` | Create enums from JSON enums with eponymous keys |
| 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`.
| 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`. |
| removeOptionalIfDefaultExists | boolean | `false` | Remove the optional modifier when a property has a default value. |
| strictIndexSignatures | boolean | `false` | Append all index signatures with `\| undefined` so that they are strictly typed. |
| 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 |
| unknownAny | boolean | `true` | Use `unknown` instead of `any` where possible |
Expand Down
3 changes: 3 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ main(
'enableConstEnums',
'format',
'ignoreMinAndMaxItems',
'removeOptionalIfDefaultExists',
'strictIndexSignatures',
'unknownAny',
'unreachableDefinitions',
Expand Down Expand Up @@ -190,6 +191,8 @@ Boolean values can be set to false using the 'no-' prefix.
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 minItems and maxItems.
--removeOptionalIfDefaultExists
Remove the optional modifier when a property has a default value
--style.XXX=YYY
Prettier configuration
--unknownAny
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ export interface Options {
* `minItems` and `maxItems`.
*/
maxItems: number
/**
* Remove the optional modifier when a property has a default value.
*/
removeOptionalIfDefaultExists: boolean
/**
* Append all index signatures with `| undefined` so that they are strictly typed.
*
Expand Down Expand Up @@ -103,6 +107,7 @@ export const DEFAULT_OPTIONS: Options = {
format: true,
ignoreMinAndMaxItems: false,
maxItems: 20,
removeOptionalIfDefaultExists: false,
strictIndexSignatures: false,
style: {
bracketSpacing: false,
Expand Down
10 changes: 7 additions & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ function parseSchema(
let asts: TInterfaceParam[] = map(schema.properties, (value, key: string) => ({
ast: parse(value, options, key, processed, usedNames),
isPatternProperty: false,
isRequired: includes(schema.required || [], key),
isRequired: includes(schema.required || [], key) || (options.removeOptionalIfDefaultExists && 'default' in value),
isUnreachableDefinition: false,
keyName: key,
}))
Expand All @@ -404,7 +404,10 @@ via the \`patternProperty\` "${key.replace('*/', '*\\/')}".`
return {
ast,
isPatternProperty: !singlePatternProperty,
isRequired: singlePatternProperty || includes(schema.required || [], key),
isRequired:
singlePatternProperty ||
includes(schema.required || [], key) ||
(options.removeOptionalIfDefaultExists && 'default' in value),
isUnreachableDefinition: false,
keyName: singlePatternProperty ? '[k: string]' : key,
}
Expand All @@ -422,7 +425,8 @@ via the \`definition\` "${key}".`
return {
ast,
isPatternProperty: false,
isRequired: includes(schema.required || [], key),
isRequired:
includes(schema.required || [], key) || (options.removeOptionalIfDefaultExists && 'default' in value),
isUnreachableDefinition: true,
keyName: key,
}
Expand Down
Loading