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

Support for readOnly properties

Open kompasbank-niels opened this issue 2 years ago • 2 comments

May I suggest a small improvement. When swagger defines a property with "readOnly":true, then the generated property should also be marked as readonly. This will help type-checking what is passed to an api, when the same type is used for input and response.

kompasbank-niels avatar Oct 09 '21 10:10 kompasbank-niels

Hello @kompasbank-niels !
Yes, that will be a good idea, but this feature should work only with specific cli option like --add-read-only

js2me avatar Oct 10 '21 00:10 js2me

While you are at it,

Don't use `object` as a type. The `object` type is currently hard to use ([see this issue](https://github.com/microsoft/TypeScript/issues/21732)).
Consider using `Record<string, unknown>` instead, as it allows you to more easily inspect and use the keys

Currently my eslinter is able to fix the readonly problem. As a build step, you can run prettier \"src/**/**.ts\" --write to add the readonly. I also had to add

"@typescript-eslint/ban-types": [
      "error",
      {
        "types": {
          "object": {
            "message": "Use Record<string, unknown> instead",
            "fixWith": "Record<string, unknown>"
          },
          "Object": {
            "message": "Use Record<string, unknown> instead",
            "fixWith": "Record<string, unknown>"
          }
        }
      }
    ]

to .eslintrc so it would be able to fix the object to its own liking.

nicobrinkkemper avatar Oct 27 '21 16:10 nicobrinkkemper