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

Typescript error with 16.9.0 (re ThunkObjMap) #4129

Closed
jeffrson opened this issue Jun 25, 2024 · 3 comments
Closed

Typescript error with 16.9.0 (re ThunkObjMap) #4129

jeffrson opened this issue Jun 25, 2024 · 3 comments

Comments

@jeffrson
Copy link

jeffrson commented Jun 25, 2024

Hi,
after upgrading to 16.9.0 our utility function for building enums shows a typescript error (using noImplicitAny). The function looks as follows:

export function buildStringEnum(name: string, values: { [key: string]: unknown }): graphql.GraphQLEnumType {
    const config: graphql.GraphQLEnumTypeConfig = { name, values: {} }

    for (const value of Object.keys(values)) {
        config.values[value] = { value }
    }

    return new graphql.GraphQLEnumType(config)
}

The error is for "config.values[value]":

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'ThunkObjMap<GraphQLEnumValueConfig>'.
  No index signature with a parameter of type 'string' was found on type 'ThunkObjMap<GraphQLEnumValueConfig>'.ts(7053)

Is this intended? And how is this supposed to be migrated?

@JoviDeCroock
Copy link
Member

JoviDeCroock commented Jun 25, 2024

Your type would be fixed by doing the following

for (const value of Object.keys(values)) {
  if (typeof config.values === 'object') {
    config.values[value] = { value }
  }
}

For some reason TypeScript gets confused and thinks you might be assigning the mapping to either a function or an object due to the addition of the possibility of assigning values through a thunk.

EDIT: Alternatively changing the type also works, this is because the upcast happens due to the explicit type of config.

const config = { name, values: {} as { [key: string]: GraphQLEnumValueConfig } }
@jeffrson
Copy link
Author

Thanks a lot for the fast and helpful reply! This fixes it.

Edit: forcing the type of config is even better! Great!

@JoviDeCroock
Copy link
Member

JoviDeCroock commented Jul 15, 2024

Going to close this issue out as it doesn't look like there's more to discuss here, I this in part comes back to GraphQL-JS exporting too many utility types 😅 the type gets expanded by casting to GraphQLEnumTypeConfig which can now be a function as value as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants