Skip to content

Tag type discrimination POC#97

Open
stackoverfloweth wants to merge 1 commit intomainfrom
tag-kind-record
Open

Tag type discrimination POC#97
stackoverfloweth wants to merge 1 commit intomainfrom
tag-kind-record

Conversation

@stackoverfloweth
Copy link
Copy Markdown
Contributor

Define kind to discriminate tags, require each kind is satisfied by setQueryData handler

Tags can be defined without kind, defaults to "default". Kind doesn't have to be unique. You can have 30 tags with "default" kind but as soon as you pass 2 tags with kind "default" in that have conflicting data types, your setQueryData is going to correctly provide and expect never.

const untypedTag = tag()
const stringTag = tag<string, 'name'>('name')
const numberTag = tag<number, 'count'>('count')

Note

This awkward syntax is a bit of a downside. Because of how TS works, we basically have to pass in the value twice, once in the generic and again at runtime. If this PR ends up being a good direction, we can talk through alt syntax that makes this less awkward

When you call setQueryData on a single tag it's a simple callback

setQueryData(numberTag, (data) => {
  expectTypeOf(data).toEqualTypeOf<number>()
  return 2
})

When you call setQueryData with multiple tags with the same kind or different kinds but same underlying data type, it's also a simple callback

const numberTag = tag<number, 'count'>('count')
const anotherNumberTag = tag<number, 'another'>('another')

setQueryData([numberTag, anotherNumberTag], (data) => {
  expectTypeOf(data).toEqualTypeOf<number>()
  return 2
})

// also ok
setQueryData([numberTag, anotherNumberTag], {
  count: (data) => {
    expectTypeOf(data).toEqualTypeOf<number>()
    return 2
  },
  another: (data) => {
    expectTypeOf(data).toEqualTypeOf<number>()
    return 42
  },
})

When you call setQueryData with multiple tags with different kinds, you MUST define a handler for each kind

setQueryData([numberTag, stringTag], {
  count: (data) => {
    expectTypeOf(data).toEqualTypeOf<number>()
    return data + 1
  },
  name: (data) => {
    expectTypeOf(data).toEqualTypeOf<string>()
    return data + 'bar'
  },
})
Screenshot 2026-05-06 at 20 54 27@2x

@stackoverfloweth stackoverfloweth requested a review from pleek91 May 7, 2026 02:01
@stackoverfloweth stackoverfloweth self-assigned this May 7, 2026
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