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

Adding unknown key to query doesn't show Typescript errors #138

Open
davidfant opened this issue Aug 31, 2023 · 3 comments
Open

Adding unknown key to query doesn't show Typescript errors #138

davidfant opened this issue Aug 31, 2023 · 3 comments

Comments

@davidfant
Copy link

davidfant commented Aug 31, 2023

I've created a React hook that wraps genql. I get TS auto-complete as expected. However, I don't get any TS errors when adding keys that don't exist (e.g. query: { somethingUnknown: true } won't cause TS errors but will cause the query to fail (obviously). It would be good if generated types would include e.g. [key: string]: never to cause TS to throw errors when requesting keys that don't exist. E.g.

interface GenQLQuery {
  knownFields: ...
  [key: string]: never;
}

Here's my hook for reference:

export function useTypedQuery<Query extends query_rootGenqlSelection>(opts: {
  name: string;
  query: Query;
  pause?: boolean;
  requestPolicy?: RequestPolicy;
  context?: Partial<OperationContext>;
}) {
  const { query, variables } = generateQueryOp({ ...opts.query, __name: opts.name });
  return useQuery<QueryResult<Query>>({
    ...opts,
    query,
    variables,
  });
}
@remorses
Copy link
Owner

remorses commented Sep 1, 2023

This is a known issue caused by TypeScript generics, Genql uses the extends syntax which allows additional keys

@jasonkuhrt
Copy link

Related #158

This should be solvable, even more so now. Prisma Client used type helpers back when to make it happen, but IIUC its easier now thanks to TS changes mentioned in linked issue.

@aydangoon
Copy link

Is there a fix for this? Running into the same issue

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

No branches or pull requests

4 participants