allen
allen2y ago

Unions of literals / Enums

Has there been any consideration to supporting enums in the schema data types? I'm referring to enum in a very loose sense here -- basically type safety that a field will have a particular value. Eg:
type MySupportedValues = 'this' | 'that';
defineSchema({
table: definteTable({
field: s.string(MySupportedValues),
}),
});
type MySupportedValues = 'this' | 'that';
defineSchema({
table: definteTable({
field: s.string(MySupportedValues),
}),
});
Then when utilizing I will not be able to specify a value for table.field that isn't "this" or "that", and when i get a table doc, I'll have table.field will have a type of MySupportedValues.
4 Replies
jamwt
jamwt2y ago
Defining a Schema | Convex Developer Hub
End-to-end type safety requires typing your tables.
jamwt
jamwt2y ago
union of s.literal might do the trick
sshader
sshader2y ago
(Also check out https://docs.convex.dev/using/schemas#infertypeof-schemavalue for a way to get MySupportedValues from a s.union(s.literal(...), ...) definition)
Defining a Schema | Convex Developer Hub
End-to-end type safety requires typing your tables.
allen
allenOP2y ago
Thanks, Jamie. I'll take a look at this. I was looking at https://docs.convex.dev/using/types
Types | Convex Developer Hub
All Convex documents are defined as Javascript objects. These objects can have

Did you find this page helpful?