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

[feature] immutable data structures

Open scottc opened this issue 5 years ago • 4 comments

Generated mutable types:

interface Thing {
  name: string;
  children: Array<Thing>; 
}

type x = ResponseWithBody<200, Array<Thing>>;

Could be immutable types:

interface ReadonlyThing {
  readonly name: string;
  readonly children: ReadonlyArray<Thing>; 
}

type x = ReadonlyResponseWithBody<200, ReadonlyArray<ReadonlyThing>>;

This would be a breaking change, so maybe mutable types by default and opt-in to immutable types, by configuration flag.

Although my personal preference would be immutable by default.

Reason being is that I'm using functional programming paradigm, where immutable data-structures are preferred, to avoid accidental state mutation as a safety feature.

scottc avatar Jun 10 '19 07:06 scottc

+1 on this! My expectation in my projects as well. I think to support backwardscompatibility we should make it an opt-in feature.

A bit of a side track - this brings up the good question on how we want to handle backwards compatibility overall. I.e. when do we want to say its ok to break something, and when do we want to keep backwards compatibility? I don't see a clear rule for this myself. @scottc, @Markionium - thoughts?

mtennoe avatar Jun 11 '19 13:06 mtennoe

IMHO, hide it behind a config flag, to opt-in as an early adopter.

And later when there is a major release, change the defaults, to opt-in by default. If there is consensus among the community.

Can have an open issue to gather community feedback and votes, for a period of time. Before making a final decision.

scottc avatar Jun 13 '19 05:06 scottc

Like it, sounds like a reasonable approach (and what many other projects do)

mtennoe avatar Jun 13 '19 06:06 mtennoe

👍

Kosta-Github avatar Jun 19 '19 14:06 Kosta-Github