Skip to content

Commit

Permalink
Fix #1383 Poor Typings for Schema
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaaym committed Nov 9, 2018
1 parent a2a32c2 commit 4fc391c
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/generator/generator.ts
Expand Up @@ -42,13 +42,30 @@ export interface GeneratorOptions {
includePrivate?: boolean;
}

function getObjectType(item: SchemaItem): string {
if (item.additionalProperties) {
const valueType = getType(item.additionalProperties);
return `{ [key: string]: ${valueType} }`;
} else if (item.properties) {
const fields = item.properties;
const objectType = Object.keys(fields)
.map(field => ` ${field}?:${getType(fields[field])}`)
.join(',');
return `{${objectType} }`;
} else {
return 'any';
}
}

function getType(item: SchemaItem): string {
if (item.$ref) {
return `Schema${item.$ref}`;
}
switch (item.type) {
case 'integer':
return 'number';
case 'object':
// TODO: This can be improved with an inline type.
return 'any';
return getObjectType(item);
case 'array':
const innerType = getType(item.items!);
return `${innerType}[]`;
Expand Down

0 comments on commit 4fc391c

Please sign in to comment.