Skip to content

Enhancement: readonly arrays #501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
laurence-myers opened this issue Mar 4, 2021 · 4 comments · Fixed by #522
Closed

Enhancement: readonly arrays #501

laurence-myers opened this issue Mar 4, 2021 · 4 comments · Fixed by #522
Labels
enhancement New feature or request good first issue Straightforward problem, solvable for first-time contributors without deep knowledge of the project PRs welcome PRs are welcome to solve this issue!

Comments

@laurence-myers
Copy link

It would be nice if we could use ReadonlyArray in place of []. Currently, trying to return a ReadonlyArray in a response will fail type checking, because the response requires a mutable array.

e.g.

    ErrorResponse: {
      //errors: components["schemas"]["Error"][]; // old way
      errors: ReadonlyArray<components["schemas"]["Error"]>; // new way
    };
@drwpow
Copy link
Contributor

drwpow commented Mar 4, 2021

Interesting! This makes sense to me.

Just for clarification: should all arrays be Readonly in TS generation? My gut instinct would be yes, but I’d like to confirm that’s what you’d think.

@drwpow drwpow added the enhancement New feature or request label Mar 4, 2021
@laurence-myers
Copy link
Author

laurence-myers commented Mar 4, 2021 via email

@drwpow
Copy link
Contributor

drwpow commented Mar 4, 2021

That’s a fair point. My initial assumption was to treat it as immutable, but now that you say that, I myself have used this library to generate new structures before sending to an API (e.g. a ReadonlyArray<> would be a pretty bad structure if you were POST-ing something).

I like the idea of a CLI flag that just turns all arrays into ReadonlyArray<>. Looking at the OpenAPI spec, I see readOnly as a schema option, but reading the description of “it MAY be sent as part of a response but SHOULD NOT be sent as part of the request” makes me think that this is something different than your idea. I think your idea is more of a TypeScript concern than a property of your OpenAPI schema, which makes me think a flag may be best.

@drwpow drwpow added PRs welcome PRs are welcome to solve this issue! good first issue Straightforward problem, solvable for first-time contributors without deep knowledge of the project labels Mar 4, 2021
@dnalborczyk
Copy link
Contributor

dnalborczyk commented Mar 10, 2021

I second this. it would be great if we could also make all types immutable, not only arrays.

e.g. graphql-code-generator for typescript has an option to generate immutable types: https://graphql-code-generator.com/docs/plugins/typescript

immutableTypes
type: boolean default: false

Generates immutable types by adding readonly to properties and uses ReadonlyArray

e.g.:

type Foo {
	readonly bar: readonly foobar[]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Straightforward problem, solvable for first-time contributors without deep knowledge of the project PRs welcome PRs are welcome to solve this issue!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants