Skip to content

Commit

Permalink
Fixed TS generator not generating union enums
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Jul 28, 2022
1 parent ff3f5d4 commit 39b45af
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/generators/typescript/renderers/EnumRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ ${this.indent(this.renderBlock(content, 2))}
}`;
}

renderUnionEnum(model: ConstrainedEnumModel): string {
const enums = model.values || [];
const enumTypes = enums.map(t => t.value).join(' | ');
return `type ${model.name} = ${enumTypes};`;
}

async renderItems(): Promise<string> {
const enums = this.model.values || [];
const items: string[] = [];
Expand All @@ -38,7 +44,10 @@ ${this.indent(this.renderBlock(content, 2))}
}

export const TS_DEFAULT_ENUM_PRESET: EnumPresetType<TypeScriptOptions> = {
self({ renderer }) {
self({ renderer, options, model }) {
if (options.enumType === 'union' && model instanceof ConstrainedEnumModel) {
return renderer.renderUnionEnum(model);
}
return renderer.defaultSelf();
},
item({ item }): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ exports[`TypeScriptGenerator should render \`enum\` type 1`] = `
}"
`;

exports[`TypeScriptGenerator should render \`enum\` type as \`union\` if option enumType = \`union\` 1`] = `"type States = States;"`;
exports[`TypeScriptGenerator should render \`enum\` type as \`union\` if option enumType = \`union\` 1`] = `"type States = \\"Texas\\" | \\"Alabama\\" | \\"California\\";"`;

exports[`TypeScriptGenerator should render \`interface\` type 1`] = `
"interface Address {
Expand Down

0 comments on commit 39b45af

Please sign in to comment.