Skip to content

bug: Overly restrictive type for filter in RefetchAfterMutationItem #986

Closed
@ericbf

Description

@ericbf

Using the object type for variables in the filter definition causes you to not be able to index the variables object for any values, nor easily define the type in the function definition. You’re forced to do a type cast to use the filter in any reasonable way. See the originating comment.

refetchAfterMutations: {
	mutation: `...`,
	// Error: Property 'id' does not exist on type 'object'.ts(2339)
	filter: (vars) => vars.id === id
}
// Error: Types of parameters 'vars' and 'variables' are incompatible.
//        Property 'id' is missing in type '{}' but required in type '{ id: string; }'.ts(2322)
refetchAfterMutations: { 
	mutation: `...`,
	filter: (vars: { id: string }) => vars.id === id
}
// Works, but is clunky
refetchAfterMutations: {
	mutation: `...`,
	filter: (vars) => (vars as { id: string }).id === id
}

Using object essentially prevents you from easily working with the variables param.

I propose we actually do use any as the type for this parameter:

export type RefetchAfterMutationItem = {
  mutation: string
  filter?: (variables: any) => boolean
}

This is saying that the filter props is a function that accepts a parameter and returns a boolean—this is essentially what we want here. If you can find a type that allows the consumer to set the type easily, that’s not any, that would also work.

As the consumer of this function, you could type it more strongly if desired:

// Would work now
refetchAfterMutations: { 
	mutation: `...`,
	filter: (vars: { id: string }) => vars.id === id
}

export type RefetchAfterMutationItem = {
mutation: string
filter?: (variables: object) => boolean
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions