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

fix type error on parsing enums #5457

Merged
merged 3 commits into from Feb 9, 2021
Merged

fix type error on parsing enums #5457

merged 3 commits into from Feb 9, 2021

Conversation

cometkim
Copy link
Contributor

@changeset-bot
Copy link

changeset-bot bot commented Jan 26, 2021

🦋 Changeset detected

Latest commit: a7d1d8c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@graphql-codegen/visitor-plugin-common Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@cometkim
Copy link
Contributor Author

Maybe It would be better to change the type of enum value to unknown. it picks a few possible type errors

❯ yarn types:check
yarn run v1.22.5
$ tsc --noEmit
packages/plugins/flow/flow/src/visitor.ts:179:15 - error TS2322: Type 'unknown' is not assignable to type 'string | number'.
  Type 'unknown' is not assignable to type 'number'.

179               enumValue = this.config.enumValues[typeName].mappedValues[enumValue];
                  ~~~~~~~~~

packages/plugins/other/visitor-plugin-common/src/base-resolvers-visitor.ts:1161:63 - error TS2345: Argument of type '{ [valueName: string]: unknown; }' is not assignable to parameter of type '{ [valueName: string]: string | number; }'.
  Index signatures are incompatible.
    Type 'unknown' is not assignable to type 'string | number'.
      Type 'unknown' is not assignable to type 'number'.

1161           ? this.buildEnumResolversExplicitMappedValues(node, this.config.enumValues[rawTypeName].mappedValues)
                                                                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

packages/plugins/other/visitor-plugin-common/src/base-types-visitor.ts:531:11 - error TS2322: Type 'unknown' is not assignable to type 'string | number'.
  Type 'unknown' is not assignable to type 'number'.

531           enumValue = this.config.enumValues[typeName].mappedValues[enumValue];
              ~~~~~~~~~

packages/plugins/typescript/typescript/src/visitor.ts:251:23 - error TS2322: Type 'unknown' is not assignable to type 'string | number'.
  Type 'unknown' is not assignable to type 'number'.

251                 const enumValue: string | number = getValueFromConfig(name) ?? name;
                          ~~~~~~~~~

packages/plugins/typescript/typescript/src/visitor.ts:271:21 - error TS2322: Type 'unknown' is not assignable to type 'string | number'.
  Type 'unknown' is not assignable to type 'number'.

271               const enumValue: string | number = valueFromConfig ?? i;
                        ~~~~~~~~~

packages/plugins/typescript/typescript/src/visitor.ts:301:21 - error TS2322: Type 'unknown' is not assignable to type 'string | number'.
  Type 'unknown' is not assignable to type 'number'.

301               const enumValue: string | number = getValueFromConfig(name) ?? name;
                        ~~~~~~~~~


Found 6 errors.

@cometkim
Copy link
Contributor Author

I can reproduce cometkim/gatsby-plugin-typegen#143 in gatsby-starter and confirmed this will fix the issue, I did test via manipulating node_modules directly

@cometkim
Copy link
Contributor Author

This type is being used in the gatsby sharp plugin.

image

@ardatan
Copy link
Collaborator

ardatan commented Feb 9, 2021

@cometkim Thanks for the PR :) For changing enum values to unknown can be discussed inside another issue :)

@ardatan ardatan merged commit 5a12fe5 into dotansimha:master Feb 9, 2021
@cometkim cometkim deleted the type-error branch February 9, 2021 16:08
ignoreEnumValuesFromSchema: false,
});

expect(result).not.toEqual({
Copy link
Contributor Author

@cometkim cometkim Feb 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, sorry about this. this test case doesn't make sense

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cometkim could you create another PR to fix that? :)

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

Successfully merging this pull request may close these issues.

None yet

2 participants