Skip to content
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

Can enumType.members allow readonly string[]? #455

Closed
MatthewHerbst opened this issue Jun 20, 2020 · 4 comments · Fixed by #916
Closed

Can enumType.members allow readonly string[]? #455

MatthewHerbst opened this issue Jun 20, 2020 · 4 comments · Fixed by #916
Labels
community/good-first-issue Good for newcomers effort/easy scope/backing-types type/improve Something existing is made better, does not affect the interface (example: better error message)

Comments

@MatthewHerbst
Copy link

MatthewHerbst commented Jun 20, 2020

I have the following data setup (not declaring as an enum because this is shared between TypeScript server code and non-TypeScript client code):

import { enumType } from 'nexus';

export const TYPE_ENUM = Object.freeze({
  H: 'H',
  S: 'S',
});
export const TYPES = Object.freeze(Object.values(TYPE_ENUM));

export const MyType = enumType({
  name: 'MyType',
  members: TYPES,
});

TypeScript is rightly giving me an error here because I'm trying to put TYPES (a readonly string[]) into members which accepts a string[].

https://github.com/graphql-nexus/schema/blob/bf0df64ebd9810f9a458aceb8c5ff52518607825/src/definitions/enumType.ts#L41-L45

To overcome the type issue the best solution I have found so far is to unwrap and then re-create the array: members: [...TYPES]

Is there a better way to overcome the type issue? If not, could enumType.members be modified to allow readonly arrays, or does members need to be mutable?

@Sytten Sytten added community/good-first-issue Good for newcomers effort/easy scope/backing-types type/improve Something existing is made better, does not affect the interface (example: better error message) labels Aug 19, 2020
@radoslavkarlik
Copy link

Any update on this? It seems to be an issue with all array types I have encountered.

@jasonkuhrt
Copy link
Member

Trying to think if there is any downside to marking the Nexus API as readonly here. I don't think so as that would only widen the accepted set of types.

@jasonkuhrt
Copy link
Member

jasonkuhrt commented Mar 23, 2021

@tgriesser WDTY? I'm sold.

@tgriesser
Copy link
Member

Yeah I think that sounds like a fine change. I wish there was a builtin utility type to express Array<T> | ReadonlyArray<T>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community/good-first-issue Good for newcomers effort/easy scope/backing-types type/improve Something existing is made better, does not affect the interface (example: better error message)
Projects
Prisma GraphQL WG Team
  
Awaiting triage
Development

Successfully merging a pull request may close this issue.

5 participants