From 0c2af620f5896c91160b797bc3ba9643a23e7f8a Mon Sep 17 00:00:00 2001 From: Antonio Calapez Date: Fri, 20 May 2022 10:11:45 +0200 Subject: [PATCH] feat(config): allow types to be used instead of interfaces --- .../pages/reference/configuration/output.md | 22 +++++++++++++++++++ src/core/generators/interface.ts | 5 ++++- src/types/index.ts | 2 ++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/docs/src/pages/reference/configuration/output.md b/docs/src/pages/reference/configuration/output.md index 4e7e78858..31bb7d35a 100644 --- a/docs/src/pages/reference/configuration/output.md +++ b/docs/src/pages/reference/configuration/output.md @@ -887,3 +887,25 @@ export function handleDates(body: any) { } } ``` + +#### useTypeOverInterfaces + +Type: `Boolean` + +Valid values: true or false. Defaults to false. + +Use this property to use TypeScript `type` instead of `interface`. + +Example: + +```js +module.exports = { + petstore: { + output: { + override: { + useTypeOverInterfaces: true, + }, + }, + }, +}; +``` diff --git a/src/core/generators/interface.ts b/src/core/generators/interface.ts index 7be16eea7..9a6e374f8 100644 --- a/src/core/generators/interface.ts +++ b/src/core/generators/interface.ts @@ -42,7 +42,10 @@ export const generateInterface = async ({ } } - if (!generalJSTypesWithArray.includes(scalar.value)) { + if ( + !generalJSTypesWithArray.includes(scalar.value) && + !context?.override?.useTypeOverInterfaces + ) { model += `export interface ${name} ${scalar.value}\n`; } else { model += `export type ${name} = ${scalar.value};\n`; diff --git a/src/types/index.ts b/src/types/index.ts index 72c41b10c..7f6276f26 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -85,6 +85,7 @@ export type NormalizedOverrideOutput = { ) => string; requestOptions: Record | boolean; useDates?: boolean; + useTypeOverInterfaces?: boolean; }; export type NormalizedMutator = { @@ -239,6 +240,7 @@ export type OverrideOutput = { ) => string; requestOptions?: Record | boolean; useDates?: boolean; + useTypeOverInterfaces?: boolean; }; type QueryOptions = {