Defining types for default values in createSelectSchema

In my users table schema for a Turso DB, I've got a field of settings: text('settings', { mode: 'json' }). I'd like to define the defaults outside of the schema definition, so this doesn't potentially change the database and require a migration when I need new setting values, but I also want to type this field in my app. So I have a userSettingsSchema and want to set the default via createSelectSchema's options:
export const userSettingsSchema = z.object({
maxOpenTodoItems: z.number().int().min(1).max(24),
});
export type UserSettings = z.infer<typeof userSettingsSchema>;

const userDataSchemaOptions = {
settings: userSettingsSchema.default(defaultUserSettings),
};

export const userDataSchema = createSelectSchema(userData, userDataSchemaOptions);
export type UserData = z.infer<typeof userDataSchema>;
export const userSettingsSchema = z.object({
maxOpenTodoItems: z.number().int().min(1).max(24),
});
export type UserSettings = z.infer<typeof userSettingsSchema>;

const userDataSchemaOptions = {
settings: userSettingsSchema.default(defaultUserSettings),
};

export const userDataSchema = createSelectSchema(userData, userDataSchemaOptions);
export type UserData = z.infer<typeof userDataSchema>;
However, under UserData, settings is still null or my UserSettings type. Wouldn't my userDataSchemaOptions have narrowed that down? It looks like I can force it more explicitly like this:
export type UserData = z.infer<typeof userDataSchema> & { settings: UserSettings };
export type UserData = z.infer<typeof userDataSchema> & { settings: UserSettings };
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server