Fix and add missing TypeScript declarations for react-relay & ..#5338
Open
christiansany wants to merge 5 commits into
Open
Fix and add missing TypeScript declarations for react-relay & ..#5338christiansany wants to merge 5 commits into
christiansany wants to merge 5 commits into
Conversation
…ionType instead of MutationParameters for better type safety
…-typescript-definitions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
First off — thank you! We've been building on Relay for nearly three years and it's been fantastic. We're especially happy that the TypeScript types are now maintained directly in this repo: it's what makes contributions like this one straightforward, and we're glad to be able to give a little back to the core team and the community. 🙏
react-relay/relay-runtimeare written in Flow but ship hand-written.d.tsfiles for TypeScript consumers. Since nothing generates them from the Flow source, they can drift. This PR fixes three such drifts (declarations only — no Flow/runtime changes).The motivating one for us was #3: a real type bug, not just a gap. Mutations using
@catchat the root level are correctly typed in Flow but were falsely rejected by the TypeScript types, which we've had to work around by overriding Relay's types in our own codebase. Fixing it at the source removes that workaround for everyone. The two missing declarations (#1) we hit along the way.1. Add two hooks that had no TypeScript types
useMutationAction_EXPERIMENTALandusePrefetchableForwardPaginationFragmentare exported fromindex.jsbut had no.d.ts, making them invisible to TypeScript. Added declarations for both and re-exported them fromhooks.d.ts.Both are constrained on
OperationType(matching the Flow sources, which don't constrain the response, and consistent with the other modern hooks).2. Add the missing
getEnvironmentForActorprop toRelayEnvironmentProviderThe Flow component accepts this optional prop but the
.d.tsomitted it, so passing it was a type error.3. Fix
MutationParametersso@catchmutations type-checkThe Flow type uses
{...}("any object") forresponse/variables/rawResponse, but the.d.tstranslated it asRecord<string, unknown>, which requires an index signature. Responses that lack one — notably theResult<…>unions from@catchmutations, and interface-typed responses — are valid in Flow but were rejected by TypeScript, forcing consumers to override Relay's types locally.Fixed by translating
{...}faithfully asobject:This only widens a constraint, so it's backward compatible — nothing that compiled before breaks. Fixing it at the source corrects the whole mutation surface at once (
commitMutation,MutationConfig,useMutation,applyOptimisticMutation, …), souseMutationitself needs no special-casing.Testing
Verified via linking the
distfiles directly in our monorepo and removing our local shims / ts overwrites that we currently have to have.