-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
cache.modify
: less strict types & dev runtime warnings
#11206
Conversation
🦋 Changeset detectedLatest commit: dba00aa The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
size-limit report 📦
|
/release:pr |
A new release has been made for this PR. You can install it with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The dev warning is really well worded - this will be a great improvement!
// @public (undocumented) | ||
type StoreObjectValueMaybeReference<StoreVal> = StoreVal extends Record<string, any>[] ? Readonly<StoreVal> | readonly Reference[] : StoreVal extends Record<string, any> ? StoreVal | Reference : StoreVal; | ||
type StoreObjectValueMaybeReference<StoreVal> = StoreVal extends Array<infer Item extends Record<string, any>> ? ReadonlyArray<AsStoreObject<Item> | Reference> : StoreVal extends Record<string, any> ? AsStoreObject<StoreVal> | Reference : StoreVal; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my angular 14 projects, compilation fails at this point with the changes in the 3.8.1 patch. Is there a workaround or documentation so I can fix it?
`Error: node_modules/@apollo/client/cache/core/types/common.d.ts:52:110 - error TS1005: '?' expected.
52 type StoreObjectValueMaybeReference = StoreVal extends Array<infer Item extends Record<string, any>> ? ReadonlyArray<AsStoreObject | Reference> : StoreVal extends Record<string, any> ? AsStoreObject | Reference : StoreVal;
`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jmoore9j what TypeScript version are you on?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@phryneas currently its at ~4.6.4. Is there a recommended version?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jmoore9j Preferrably a recent one - TypeScript moves quite fast, and I would almost bet that you wouldn't have that problem with 5.1 or 5.2.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The specific notion that's giving you problems here (infer X extends Y
) seems to have been added with TS 4.8
Checklist:
This fixes #11171.
While technically it is correct that the
modify
function should always return an array of only objects or only references, this is very annoying to be used with TypeScript - and also never enforced by us on Runtime.So instead, this PR relaxes the strictness of the
modify
arguments fromObj[] | Reference[]
to(Obj | Reference)[]
, and does the same for the return value.Additionally, it adds a runtime warning if the
modify
function returns a mixed array, where at least one of the non-Reference-objects could be represented by a Reference.Also, there was a footgun of only calling
toReference(obj)
whereobj
was not part of the store yet. In that case, it would not writeobj
to the store and create a "dead" reference. I also added a DEV-level warning for those cases.