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 link/core/types to be compatible with graphql typings while TypeScript exactOptionalPropertyTypes mode is enabled #10497

Merged
merged 11 commits into from
Feb 10, 2023

Conversation

nevir
Copy link
Contributor

@nevir nevir commented Jan 30, 2023

When importing @apollo/client in a project with TS 4.9, graphql 16.6 and strict: true and exactOptionalPropertyTypes: true, we see the following compilation error:

./node_modules/@apollo/client/link/core/types.d.ts(44,18): error TS2430: Interface 'SingleExecutionResult<TData, TContext, TExtensions>' incorrectly extends interface 'ExecutionResult<TData, TExtensions>'.
  Types of property 'data' are incompatible.
    Type 'Data<TData>' is not assignable to type 'TData | null'.
      Type 'undefined' is not assignable to type 'TData | null'.

Because Data<TData> expands to:

  data?: TData | undefined | null

Which is subtly different from:

  data?: TData | null

E.g. the GraphQL types are declaring that data can be not present in the object, but it should not be explicitly { data: undefined }.

For extra context, check the TypeScript docs on the exactOptionalPropertyTypes flag


This fixes the type to be compatible.

@changeset-bot
Copy link

changeset-bot bot commented Jan 30, 2023

🦋 Changeset detected

Latest commit: 4d552b2

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

This PR includes changesets to release 1 package
Name Type
@apollo/client 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

…cript strict mode is on

When importing `@apollo/client` in a project with TS 4.9, graphql 16.6 and `strict: true`, we see the following compilation error:

```
./node_modules/@apollo/client/link/core/types.d.ts(44,18): error TS2430: Interface 'SingleExecutionResult<TData, TContext, TExtensions>' incorrectly extends interface 'ExecutionResult<TData, TExtensions>'.
  Types of property 'data' are incompatible.
    Type 'Data<TData>' is not assignable to type 'TData | null'.
      Type 'undefined' is not assignable to type 'TData | null'.
```

Because `Data<TData>` expands to:
```ts
  data?: TData | undefined | null
```

Which is subtly different from:
```ts
  data?: TData | null
```

This fixes the type to be compatible.
@alessbell
Copy link
Member

Hi @nevir 👋 I'd like to understand the problem a bit better; I've tried to reproduce this and wasn't able to in https://github.com/apollographql/router-defer-e2e-tests.

If you clone that repo, cd into CRA-demo, npm i && npx tsc you should see 0 problems reported by TS. That project is using TS 4.9, graphql 16.6 and both strict: true and exactOptionalPropertyTypes: true as you've mentioned. If you have further reproduction steps/a reproduction I can look at that would be a huge help, thanks!

@alessbell alessbell added the 🏓 awaiting-contributor-response requires input from a contributor label Jan 31, 2023
@alessbell
Copy link
Member

Looks like FetchResult.data was updated to explicitly include undefined in AC 3.5.0: 24799a9#diff-5e2dbe69feb4479799e4247c4d09edd3c9f6255ce84522aece23a5478cf2644aR28

Removing undefined from its possible values (while keeping the data key as optional) would technically be a breaking change, but it's one I'm in favor of. I propose targeting this at the next minor release (3.8) - thoughts @jerelmiller @phryneas?

@alessbell alessbell removed the 🏓 awaiting-contributor-response requires input from a contributor label Feb 8, 2023
Copy link
Member

@alessbell alessbell left a comment

Choose a reason for hiding this comment

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

LGTM ✅

@alessbell alessbell self-assigned this Feb 10, 2023
@alessbell alessbell merged commit 8a883d8 into apollographql:main Feb 10, 2023
@github-actions github-actions bot mentioned this pull request Feb 10, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants