Skip to content

Empty variables, consider using never type? #147

Open
@christoomey

Description

@christoomey

First, thank you so much for this wonderful project! It's extremely impressive and I've been very happy with it overall.

I did run into one small issue for a query with no variables, e.g.

const QUERY = gql`
  query Users {
    users {
      id
      name
    }
  }
`

For this query, the generated type of the UsersVariables would be:

export type UsersVariables = {};

Unfortunately, TypeScript is a bit overly permissive with the {} literal value and will allow any object to be assigned:

type UsersVariables = {}

// The following is incorrect as the query takes no variables,
// but the typing allows it
const variables: UsersVariables = { name: 'hello' }

// Using a more restrictive type, we can prevent this:
type UsersVariablesNever = { [index: string]: never };
// This line will now properly highlight the `name` value
// as an error since the query doesn't accept any variables
const neverVariables: UsersVariablesNever = { name: 'hello' }

TypeScript playground with this example


My suggestion would be to use the more restrictive { [index: string]: never } instead of {} for empty variable type definitions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions