Cannot query with sum type variant index
Say I have
This works, but trying to query using that index fails with a type error
I expected the above to work as though the following were true (though the following is not valid)
So I found this somewhat surprising.
// convex/schema.ts
export default defineSchema({
pets: defineTable(
v.union(
v.object({
tag: v.literal("Dog"),
favoriteToy: v.optional(v.string())
}),
v.object({
tag: v.literal("Cat")
})
)
)
.index("by_favorite_toy", ["favoriteToy"]),
})// convex/schema.ts
export default defineSchema({
pets: defineTable(
v.union(
v.object({
tag: v.literal("Dog"),
favoriteToy: v.optional(v.string())
}),
v.object({
tag: v.literal("Cat")
})
)
)
.index("by_favorite_toy", ["favoriteToy"]),
})This works, but trying to query using that index fails with a type error
db
.query("orders")
.withIndex("by_favorite_toy", (q) =>
q.eq("favoriteToy", favorite_toy)
)
.unique()db
.query("orders")
.withIndex("by_favorite_toy", (q) =>
q.eq("favoriteToy", favorite_toy)
)
.unique()error TS2345: Argument of type 'string' is not assignable to parameter of type 'undefined'.
q.eq("favoriteToy", favoriteToy),
~~~~~~~~~~~error TS2345: Argument of type 'string' is not assignable to parameter of type 'undefined'.
q.eq("favoriteToy", favoriteToy),
~~~~~~~~~~~I expected the above to work as though the following were true (though the following is not valid)
// convex/schema.ts
export default defineSchema({
pets: defineTable(
v.union(
v.object({
tag: v.literal("Dog"),
favoriteToy: v.optional(v.string())
}),
v.object({
tag: v.literal("Cat"),
favoriteToy: v.literal(undefined) // `undefined` not an accepted literal
})
)
)
.index("by_favorite_toy", ["favoriteToy"]),
})// convex/schema.ts
export default defineSchema({
pets: defineTable(
v.union(
v.object({
tag: v.literal("Dog"),
favoriteToy: v.optional(v.string())
}),
v.object({
tag: v.literal("Cat"),
favoriteToy: v.literal(undefined) // `undefined` not an accepted literal
})
)
)
.index("by_favorite_toy", ["favoriteToy"]),
})So I found this somewhat surprising.
