Skip to content

Tag type discrimination POC#99

Draft
stackoverfloweth wants to merge 4 commits into
mainfrom
tag-kind-record
Draft

Tag type discrimination POC#99
stackoverfloweth wants to merge 4 commits into
mainfrom
tag-kind-record

Conversation

@stackoverfloweth
Copy link
Copy Markdown
Member

Currently a tag can only have 1 possible type

const numberTab = tab<number>()

This PR introduces an alternative generic to tag that enables a single tag to have many possible sub-types.

const numberOrStringTag = tag<{
  count: number,
  name: string
}>(['count', 'name'])

Here's a real world example

type User = { id: string, name: string, email: string }
type UserAvatar = { id: string, imageUrl: string }

const userTag = tag<{
  user: User,
  avatar: UserAvatar
}>()

const userQuery = query(getUser, [userId], { tags: [userTag.user] })
const userAvatarQuery = query(getUserAvatar, [userId], { tags: [userTag.avatar] })

When invalidating queries, devs can target whatever layer they want and it will trickle down

// only invalidates the avatar queries
client.invalidateQueries(userTag.avatar)

// invalidates all 3
client.invalidateQueries(userTag)

When performing an optimistic update on a tag with multiple sub-types, the callback will require that each kind be handled

setQueryData(genericTag, {
  user: (data) => {
    expectTypeOf(data).toEqualTypeOf<User>()
    return data
  },
  avatar: (data) => {
    expectTypeOf(data).toEqualTypeOf<UserAvatar>()
    return data
  },
})

@stackoverfloweth stackoverfloweth self-assigned this May 9, 2026
@stackoverfloweth stackoverfloweth changed the title Tag kind record Tag type discrimination POC May 9, 2026
@stackoverfloweth stackoverfloweth requested a review from pleek91 May 9, 2026 13:34
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

Successfully merging this pull request may close these issues.

1 participant