swagger-typescript-api
swagger-typescript-api copied to clipboard
Option to generate types instead of interfaces
Hi!
Is it possible to add an option to generate
export type User = {
username?: string;
uuid?: string;
}
instead of
export interface User {
username?: string;
uuid?: string;
}
Sometimes it is necessary to prevent possible expansion of the type. Use case here
How would you differentiate input into each?
I did not understand the question. The input is the same. Like for --union-enums input is the same, but output depends from flag value.
As I understand it you would like this switch to be global. Right? My thoughts were in how would you differentiate which model should map to type and which to interface but I see you would like to have them all be a type.
Right
I did this in my project by adding a data-contracts.eta template and overwriting the default dataContractTemplates object with this:
const dataContractTemplates = {
enum: (contract) => {
return `enum ${contract.name} {\r\n${contract.content} \r\n }`;
},
// Force all interfaces to type
interface: (contract) => {
return `type ${contract.name} = {\r\n ${contract.content} \r\n}`;
},
type: (contract) => {
return `type ${contract.name} = ${contract.content}`;
},
}
Having interfaces returned instead of types from requests can cause bugs if you have interfaces with overlapping required methods or interfaces with purely optional methods
Just spent some minutes-too-many to discover I was calling the wrong fetch because of interfaces, after I manually switched to types vscode caught the error