Skip to content

Commit ddbd627

Browse files
authored
Merge pull request #252 from G-Rath/implement-strictIndexSignatures-option
Implement strict index signatures option
2 parents 74569c8 + 8c14a5c commit ddbd627

File tree

8 files changed

+54
-1
lines changed

8 files changed

+54
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
.DS_Store
2+
.idea/
23
npm-debug.log
34
node_modules
45
*.js
56
*.map
67
dist/
7-
dist_tests/
8+
dist_tests/

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.DS_Store
2+
.idea
23
.vscode
34
npm-debug.log
45
node_modules

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ See [server demo](example) and [browser demo](https://github.com/bcherny/json-sc
8989
| enableConstEnums | boolean | `true` | Prepend enums with [`const`](https://www.typescriptlang.org/docs/handbook/enums.html#computed-and-constant-members)? |
9090
| 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 |
9191
| unreachableDefinitions | boolean | `false` | Generates code for `definitions` that aren't referenced by the schema. |
92+
| strictIndexSignatures | boolean | `false` | Append all index signatures with `| undefined` so that they are strictly typed. |
9293
| $refOptions | object | `{}` | [$RefParser](https://github.com/BigstickCarpet/json-schema-ref-parser) Options, used when resolving `$ref`s |
9394
## CLI
9495

src/generator.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,16 @@ function declareNamedTypes(
145145
}
146146

147147
function generateType(ast: AST, options: Options): string {
148+
const type = generateRawType(ast, options)
149+
150+
if (options.strictIndexSignatures && ast.keyName === '[k: string]') {
151+
return `${type} | undefined`
152+
}
153+
154+
return type
155+
}
156+
157+
function generateRawType(ast: AST, options: Options): string {
148158
log(whiteBright.bgMagenta('generator'), ast)
149159

150160
if (hasStandaloneName(ast)) {

src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ export interface Options {
3232
* Prepend enums with [`const`](https://www.typescriptlang.org/docs/handbook/enums.html#computed-and-constant-members)?
3333
*/
3434
enableConstEnums: boolean
35+
/**
36+
* Append all index signatures with `| undefined` so that they are strictly typed.
37+
*
38+
* This is required to be compatible with `strictNullChecks`.
39+
*/
40+
strictIndexSignatures: boolean
3541
/**
3642
* A [Prettier](https://prettier.io/docs/en/options.html) configuration.
3743
*/
@@ -57,6 +63,7 @@ export const DEFAULT_OPTIONS: Options = {
5763
cwd: process.cwd(),
5864
declareExternallyReferenced: true,
5965
enableConstEnums: true, // by default, avoid generating code
66+
strictIndexSignatures: false,
6067
style: {
6168
bracketSpacing: false,
6269
printWidth: 120,

test/__snapshots__/test/test.ts.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9425,3 +9425,20 @@ Generated by [AVA](https://ava.li).
94259425
[k: string]: any;␊
94269426
}␊
94279427
`
9428+
9429+
## strictIndexSignatures.js
9430+
9431+
> Snapshot 1
9432+
9433+
`/* tslint:disable */␊
9434+
/**␊
9435+
* This file was automatically generated by json-schema-to-typescript.␊
9436+
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,␊
9437+
* and run json-schema-to-typescript to regenerate this file.␊
9438+
*/␊
9439+
9440+
export interface StrictIndexSignatures {␊
9441+
maybe?: string;␊
9442+
[k: string]: string | undefined;␊
9443+
}␊
9444+
`

test/__snapshots__/test/test.ts.snap

245 Bytes
Binary file not shown.

test/e2e/strictIndexSignatures.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export const input = {
2+
title: 'StrictIndexSignatures',
3+
type: 'object',
4+
properties: {
5+
maybe: {
6+
type: 'string'
7+
}
8+
},
9+
additionalProperties: {
10+
type: 'string'
11+
}
12+
}
13+
14+
export const options = {
15+
strictIndexSignatures: true
16+
}

0 commit comments

Comments
 (0)