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

cache.identify : Index signature is missing in type #7577

Closed
jimmycrequer opened this issue Jan 14, 2021 · 8 comments
Closed

cache.identify : Index signature is missing in type #7577

jimmycrequer opened this issue Jan 14, 2021 · 8 comments

Comments

@jimmycrequer
Copy link

jimmycrequer commented Jan 14, 2021

related to

#6083

Intended outcome:

I want to retrieve the Apollo Client cache ID for a particular object.

interface MyMapInterface {
  __typename: "Map";
  id: string;
  width: number;
  height: number;
}

const myMap: MyMapInterface = { __typename: "Map", id: "1", width: 100, height: 100 };

client.cache.identify(myMap);

Actual outcome:

I get the following TS error.

Argument of type 'MyInterface' is not assignable to parameter of type 'StoreObject | Reference'.
  Type 'MyInterface' is not assignable to type 'StoreObject'.
    Index signature is missing in type 'MyInterface'.

How to reproduce the issue:

Playground

Versions

  System:
    OS: Windows 10 10.0.19041
  Binaries:
    Node: 15.5.1 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.10 - ~\Documents\workspace\GOunite\v3\client\node_modules\.bin\yarn.CMD
    npm: 6.14.10 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: 87.0.4280.88
    Edge: Spartan (44.19041.423.0), Chromium (87.0.664.75)
  npmPackages:
    @apollo/client: 3.3.6 => 3.3.6
    @apollo/link-context: 2.0.0-beta.3 => 2.0.0-beta.3
    @apollo/link-error: 2.0.0-beta.3 => 2.0.0-beta.3
    @apollo/link-persisted-queries: 1.0.0-beta.0 => 1.0.0-beta.0
    @apollo/link-ws: 2.0.0-beta.3 => 2.0.0-beta.3
    @apollo/react-testing: 3.1.4 => 3.1.4
    apollo: 2.32.1 => 2.32.1
    apollo-cache-persist: 0.1.1 => 0.1.1
    apollo-upload-client: 14.1.0 => 14.1.0

TS 4.1

@benjamn
Copy link
Member

benjamn commented Jan 14, 2021

@jimmycrequer Hmm, it's disappointing that TypeScript is so strict about this, since MyMapInterface definitely seems compatible with StoreObject (as far as cache.identify is concerned):

export interface StoreObject {
  __typename?: string;
  [storeFieldName: string]: StoreValue;
}

As a workaround, you might try adding a [key: string]: any field to MyMapInterface, to make it look more like StoreObject. I realize that's not ideal, but it could get you unstuck for now.

On the implementation side, I'm open to relaxing the cache.identify parameter type to something like object, since it can always fail (return undefined) for objects that are not identifiable.

@AdrienLemaire
Copy link

AdrienLemaire commented Mar 17, 2021

As a workaround, you might try adding a [key: string]: any field to MyMapInterface, to make it look more like StoreObject. I realize that's not ideal, but it could get you unstuck for now.

This is a bit difficult when using auto-generated codegen types from neo4j-graphql-js.
@benjamn @johnymontana would it be appropriate to ask neo4j-graphql-js to export types with [storeFieldName: string]: StoreValue; by default ?

For the time being, I'll cast the type:

type StoreObjectMap = getMap_map & {
  [storeFieldName: string]: StoreValue;
};

const map = (data.Map[0].map as unknown) as StoreObjectMap;
const mapRef = client.cache.identify(map);

Hopefully we can clean this soon

@rylanc
Copy link

rylanc commented Apr 27, 2021

The same issue exists if the interface passed is generated by the Apollo CLI (e.g. using an object returned in the data of a mutation). For best compatibility between Apollo created tools, it seems like changing the parameter type might be the best fix.

@jcf120
Copy link

jcf120 commented Aug 13, 2021

Any updates? Would be lovely if I didn't have to cast anymore :)

@enheit
Copy link

enheit commented Oct 9, 2021

I have the same issue

Argument of type 'DeclineAppointment_updateAppointment' is not assignable to parameter of type 'Reference | StoreObject'.
  Type 'DeclineAppointment_updateAppointment' is not assignable to type 'StoreObject'.
    Index signature for type 'string' is missing in type 'DeclineAppointment_updateAppointment'.  TS2345

@PeterDekkers
Copy link

PeterDekkers commented May 24, 2022

It's possible to circumvent the issue by using spread syntax.

client.cache.identify({ ...myMap });

Although this could affect performance. Personally I prefer to @ts-ignore the warning.

There is a related issue in the Typescript repo.

@phryneas
Copy link
Member

This is unfortunately a difference between TypeScript type and interface declarations - a type will always work, but an interface will not be accepted as it is missing that implicit index signature

Apollo Client is now exporting the AsStoreObject type from @apollo/client/utilities as a workaround:

const myMap: AsStoreObject<MyMapInterface> = ...

(Alternatively you could declare MyMapInterface as a type, not an interface.)

I believe that's all we can do on our side, so I'll close this issue :)

Copy link
Contributor

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
For general questions, we recommend using StackOverflow or our discord server.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

8 participants