swagger-typescript-api icon indicating copy to clipboard operation
swagger-typescript-api copied to clipboard

Option to generate types instead of interfaces

Open dartess opened this issue 4 years ago • 6 comments

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

dartess avatar Jul 19 '21 15:07 dartess

How would you differentiate input into each?

hi502 avatar Jul 28 '21 11:07 hi502

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.

dartess avatar Jul 31 '21 11:07 dartess

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.

hi502 avatar Aug 02 '21 08:08 hi502

Right

dartess avatar Aug 02 '21 09:08 dartess

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}`;
    },
  }

slinkardbrandon avatar Oct 15 '21 18:10 slinkardbrandon

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

localhosted avatar Oct 22 '21 11:10 localhosted