Skip to content

Implement strict index signatures option #252

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

Merged
merged 5 commits into from
Sep 7, 2019
Merged
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 .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
.DS_Store
.idea/
npm-debug.log
node_modules
*.js
*.map
dist/
dist_tests/
dist_tests/
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
.idea
.vscode
npm-debug.log
node_modules
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ See [server demo](example) and [browser demo](https://github.com/bcherny/json-sc
| enableConstEnums | boolean | `true` | Prepend enums with [`const`](https://www.typescriptlang.org/docs/handbook/enums.html#computed-and-constant-members)? |
| 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 |
| unreachableDefinitions | boolean | `false` | Generates code for `definitions` that aren't referenced by the schema. |
| strictIndexSignatures | boolean | `false` | Append all index signatures with `| undefined` so that they are strictly typed. |
| $refOptions | object | `{}` | [$RefParser](https://github.com/BigstickCarpet/json-schema-ref-parser) Options, used when resolving `$ref`s |
## CLI

Expand Down
10 changes: 10 additions & 0 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ function declareNamedTypes(
}

function generateType(ast: AST, options: Options): string {
const type = generateRawType(ast, options)

if (options.strictIndexSignatures && ast.keyName === '[k: string]') {
return `${type} | undefined`
}

return type
}

function generateRawType(ast: AST, options: Options): string {
log(whiteBright.bgMagenta('generator'), ast)

if (hasStandaloneName(ast)) {
Expand Down
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ export interface Options {
* Prepend enums with [`const`](https://www.typescriptlang.org/docs/handbook/enums.html#computed-and-constant-members)?
*/
enableConstEnums: boolean
/**
* Append all index signatures with `| undefined` so that they are strictly typed.
*
* This is required to be compatible with `strictNullChecks`.
*/
strictIndexSignatures: boolean
/**
* A [Prettier](https://prettier.io/docs/en/options.html) configuration.
*/
Expand All @@ -57,6 +63,7 @@ export const DEFAULT_OPTIONS: Options = {
cwd: process.cwd(),
declareExternallyReferenced: true,
enableConstEnums: true, // by default, avoid generating code
strictIndexSignatures: false,
style: {
bracketSpacing: false,
printWidth: 120,
Expand Down
17 changes: 17 additions & 0 deletions test/__snapshots__/test/test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -9425,3 +9425,20 @@ Generated by [AVA](https://ava.li).
[k: string]: any;␊
}␊
`

## strictIndexSignatures.js

> Snapshot 1

`/* tslint: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 StrictIndexSignatures {␊
maybe?: string;␊
[k: string]: string | undefined;␊
}␊
`
Binary file modified test/__snapshots__/test/test.ts.snap
Binary file not shown.
16 changes: 16 additions & 0 deletions test/e2e/strictIndexSignatures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const input = {
title: 'StrictIndexSignatures',
type: 'object',
properties: {
maybe: {
type: 'string'
}
},
additionalProperties: {
type: 'string'
}
}

export const options = {
strictIndexSignatures: true
}